﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using WHW_NetUtility;
using System.Runtime.Serialization.Formatters.Binary;
using MotionControl;
using System.Threading;
using System.Diagnostics;
using System.Threading.Tasks;
using Newtonsoft.Json;
using HalconDotNet;
using XXTUserUI;

namespace MainFrame
{
    public partial class FormManual : Form
    {
        string saveFilePath = "";
        public FormManual()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 设置撕膜限位
        /// </summary>
        private void SetCutLimit()
        {
            
            var cutX_min = -264;
            var cutX_max =0;
            nud_cut_WaitX.Minimum = cutX_min;
            nud_cut_WaitX.Maximum = cutX_max;

            nud_cut_ClampX.Minimum = cutX_min;
            nud_cut_ClampX.Maximum = cutX_max;

            nud_cut_mediumX.Minimum = cutX_min;
            nud_cut_mediumX.Maximum = cutX_max;

            nud_cut_EndX.Minimum = cutX_min;
            nud_cut_EndX.Maximum = cutX_max;


            nud_cut_NGX.Minimum = cutX_min;
            nud_cut_NGX.Maximum = cutX_max;

            nud_cut_ProX.Minimum = cutX_min;
            nud_cut_ProX.Maximum = cutX_max;

            var cutY_min = 0;
            var cutY_max = 299;
            nud_cut_WaitY.Minimum = cutY_min;
            nud_cut_WaitY.Maximum = cutY_max;

            nud_cut_ClampY.Minimum = cutY_min;
            nud_cut_ClampY.Maximum = cutY_max;

            nud_cut_mediumY.Minimum = cutY_min;
            nud_cut_mediumY.Maximum = cutY_max;

            nud_cut_EndY.Minimum = cutY_min;
            nud_cut_EndY.Maximum = cutY_max;


            nud_cut_NGY.Minimum = cutY_min;
            nud_cut_NGY.Maximum = cutY_max;

            nud_cut_ProY.Minimum = cutY_min;
            nud_cut_ProY.Maximum = cutY_max;

            var cutZ_min = 0;
            var cutZ_max = 85;
            nud_cut_WaitZ.Minimum = cutZ_min;
            nud_cut_WaitZ.Maximum = cutZ_max;

            nud_cut_ClampZ.Minimum = cutZ_min;
            nud_cut_ClampZ.Maximum = cutZ_max;

            nud_cut_mediumZ.Minimum = cutZ_min;
            nud_cut_mediumZ.Maximum = cutZ_max;

            nud_cut_EndZ.Minimum = cutZ_min;
            nud_cut_EndZ.Maximum = cutZ_max;


            nud_cut_NGZ.Minimum = cutZ_min;
            nud_cut_NGZ.Maximum = cutZ_max;

            nud_cut_ProZ.Minimum = cutZ_min;
            nud_cut_ProZ.Maximum = cutZ_max;

            var cutR1_min = 0;
            var cutR1_max = 359;
            nud_cut_WaitR1.Minimum = cutR1_min;
            nud_cut_WaitR1.Maximum = cutR1_max;

            nud_cut_ClampR1.Minimum = cutR1_min;
            nud_cut_ClampR1.Maximum = cutR1_max;

            nud_cut_mediumR1.Minimum = cutR1_min;
            nud_cut_mediumR1.Maximum = cutR1_max;

            nud_cut_EndR1.Minimum = cutR1_min;
            nud_cut_EndR1.Maximum = cutR1_max;


            nud_cut_NGR1.Minimum = cutR1_min;
            nud_cut_NGR1.Maximum = cutR1_max;

            nud_cut_ProR1.Minimum = cutR1_min;
            nud_cut_ProR1.Maximum = cutR1_max;

            var cutR2_min = 0;
            var cutR2_max = 359;
            nud_cut_WaitR2.Minimum = cutR2_min;
            nud_cut_WaitR2.Maximum = cutR2_max;

            nud_cut_ClampR2.Minimum = cutR2_min;
            nud_cut_ClampR2.Maximum = cutR2_max;

            nud_cut_mediumR2.Minimum = cutR2_min;
            nud_cut_mediumR2.Maximum = cutR2_max;

            nud_cut_EndR2.Minimum = cutR2_min;
            nud_cut_EndR2.Maximum = cutR2_max;


            nud_cut_NGR2.Minimum = cutR2_min;
            nud_cut_NGR2.Maximum = cutR2_max;

            nud_cut_ProR2.Minimum = cutR2_min;
            nud_cut_ProR2.Maximum = cutR2_max;


            nud_downProZ.Minimum = -65;
            nud_downProZ.Maximum =0;
            nud_upProZ.Minimum = -65;
            nud_upProZ.Maximum = 0;
         
        }
        private void SetLabelLimit(int nPlate)
        {


            //var GetLabelXmax = 0;
            //var GetLabelXmin = -100;
            //var GetLabelYmax = 50;
            //var GetLabelYmin = 0;
            //var GetLabelRmax = 4;
            //var GetLabelRmin = -30;
            //var GetLabelZmax = 40;
            //var GetLabelZmin =0;

            //var Pos_ScanXmax = -100;
            Limit.Pos_ScanXmin = -200;
            //var Pos_ScanYmax = 300;
            //var Pos_ScanYmin = 100;
            //var Pos_ScanZmax = 10;
            //var Pos_ScanZmin = 0;

            //var PosNGX_Max= 0;
            //var PosNGX_Min = -50;
            //var PosNGY_Max = 300;
            //var PosNGY_Min =100;
            //var PosNGZ_Max =130;
            //var PosNGZ_Min = 0;
            //var PosNGZHeight_Max =30;
            //var PosNGZHeight_Min = 0;
            //var PosNGR_Max = 0;
            //var PosNGR_Min = -100;

            //var PosOKX_Max = 0;
            Limit.PosOKX_Min = -210;
            //var PosOKY_Max = 550;
            //var PosOKY_Min = 500;
            //var PosOKZ_Max = 230;
            //var PosOKZ_Min = 100;
            //var PosOKZHeight_Max =100;
            //var PosOKZHeight_Min = 0;
            //var PosOKR_Max = 0;
            //var PosOKR_Min = -100;
            try
            {
                if (nPlate == (int)e_platePro.平台2)
                {
                    Limit.Pos_ScanXmin = -230;
                    Limit.PosOKX_Min = -230;
                }
                //nud_GetLabelX.Maximum = GetLabelXmax;
                //nud_GetLabelX.Minimum = GetLabelXmin;
                //nud_GetLabelY.Maximum = GetLabelYmax;
                //nud_GetLabelY.Minimum = GetLabelYmin;
                //nud_GetLabelR.Maximum = GetLabelRmax;
                //nud_GetLabelR.Minimum = GetLabelRmin;
                //nud_GetLabelZ.Maximum = GetLabelZmax;
                //nud_GetLabelZ.Minimum = GetLabelZmin;
                //nud_GetLabelZHeight.Maximum = GetLabelZmax;
                //nud_GetLabelZHeight.Minimum = GetLabelZmin;

                //nud_pos_scanX.Maximum = Pos_ScanXmax;
                //nud_pos_scanX.Minimum = Pos_ScanXmin;
                //nud_pos_scanY.Maximum = Pos_ScanYmax;
                //nud_pos_scanY.Minimum = Pos_ScanYmin;
                //nud_pos_scanZ.Maximum = Pos_ScanZmax;
                //nud_pos_scanZ.Minimum = Pos_ScanZmin;

                //nud_pos_NGY.Maximum = PosNGY_Max;
                //nud_pos_NGY.Minimum = PosNGY_Min;
                //nud_pos_NGX.Maximum = PosNGX_Max;
                //nud_pos_NGX.Minimum = PosNGX_Min;
                //nud_pos_NGZ.Maximum = PosNGZ_Max;
                //nud_pos_NGZ.Minimum = PosNGZ_Min;
                //nud_pos_NGR.Maximum = PosNGR_Max;
                //nud_pos_NGR.Minimum = PosNGR_Min;
                //nud_pos_NGZHeight.Maximum = PosNGZHeight_Max;
                //nud_pos_NGZHeight.Minimum = PosNGZHeight_Min;

                //nud_pos_OKY.Maximum = PosOKY_Max;
                //nud_pos_OKY.Minimum = PosOKY_Min;
                //nud_pos_OKX.Maximum = PosOKX_Max;
                //nud_pos_OKX.Minimum = PosOKX_Min;
                //nud_pos_OKZ.Maximum = PosOKZ_Max;
                //nud_pos_OKZ.Minimum = PosOKZ_Min;
                //nud_pos_OKR.Maximum = PosOKR_Max;
                //nud_pos_OKR.Minimum = PosOKR_Min;
                //nud_pos_OKZHeight.Maximum = PosOKZHeight_Max;
                //nud_pos_OKZHeight.Minimum = PosOKZHeight_Min;
                nud_GetLabelX.Maximum = Limit.GetLabelXmax;
                nud_GetLabelX.Minimum = Limit.GetLabelXmin;
                nud_GetLabelY.Maximum = Limit.GetLabelYmax;
                nud_GetLabelY.Minimum = Limit.GetLabelYmin;
                nud_GetLabelR.Maximum = Limit.GetLabelRmax;
                nud_GetLabelR.Minimum = Limit.GetLabelRmin;
                nud_GetLabelZ.Maximum = Limit.GetLabelZmax;
                nud_GetLabelZ.Minimum = Limit.GetLabelZmin;
                nud_GetLabelZHeight.Maximum = Limit.GetLabelZmax;
                nud_GetLabelZHeight.Minimum = Limit.GetLabelZmin;

                nud_pos_scanX.Maximum = Limit.Pos_ScanXmax;
                nud_pos_scanX.Minimum = Limit.Pos_ScanXmin;
                nud_pos_scanY.Maximum = Limit.Pos_ScanYmax;
                nud_pos_scanY.Minimum = Limit.Pos_ScanYmin;
                nud_pos_scanZ.Maximum = Limit.Pos_ScanZmax;
                nud_pos_scanZ.Minimum = Limit.Pos_ScanZmin;

                nud_pos_NGY.Maximum = Limit.PosNGY_Max;
                nud_pos_NGY.Minimum = Limit.PosNGY_Min;
                nud_pos_NGX.Maximum = Limit.PosNGX_Max;
                nud_pos_NGX.Minimum = Limit.PosNGX_Min;
                nud_pos_NGZ.Maximum = Limit.PosNGZ_Max;
                nud_pos_NGZ.Minimum = Limit.PosNGZ_Min;
                nud_pos_NGR.Maximum = Limit.PosNGR_Max;
                nud_pos_NGR.Minimum = Limit.PosNGR_Min;
                nud_pos_NGZHeight.Maximum = Limit.PosNGZHeight_Max;
                nud_pos_NGZHeight.Minimum = Limit.PosNGZHeight_Min;

                nud_pos_OKY.Maximum = Limit.PosOKY_Max;
                nud_pos_OKY.Minimum = Limit.PosOKY_Min;
                nud_pos_OKX.Maximum = Limit.PosOKX_Max;
                nud_pos_OKX.Minimum = Limit.PosOKX_Min;
                nud_pos_OKZ.Maximum = Limit.PosOKZ_Max;
                nud_pos_OKZ.Minimum = Limit.PosOKZ_Min;
                nud_pos_OKR.Maximum = Limit.PosOKR_Max;
                nud_pos_OKR.Minimum = Limit.PosOKR_Min;
                nud_pos_OKZHeight.Maximum = Limit.PosOKZHeight_Max;
                nud_pos_OKZHeight.Minimum = Limit.PosOKZHeight_Min;
            }
            catch(Exception ex)
            {
                displalyInfo.displayErrorMain(ex.Message);
            }
          
        }
        private int posCount = 0;
        private int posNGCount = 0;
        public drawInfo GetDrawInfo()
        {
            var curInfo = new drawInfo();
            curInfo.d_AreaMax = (int)nud_d_AreaMax.Value;
            curInfo.d_AreaMin = (int)nud_d_AreaMin.Value;
            curInfo.d_grayMax = (int)nud_d_grayMax.Value;
            curInfo.d_grayMin = (int)nud_d_grayMin.Value;
            return curInfo;
        }
        public void SetDrawInfo(drawInfo curInfo)
        {
            if(curInfo==null)
             curInfo = new drawInfo();
            nud_d_AreaMax.Value = curInfo.d_AreaMax;
            nud_d_AreaMin.Value = curInfo.d_AreaMin;
            nud_d_grayMax.Value = curInfo.d_grayMax;
            nud_d_grayMin.Value = curInfo.d_grayMin;
         
        }
        private void ControlToParameter()
        {
            try
            {
                if (GlobalVar.motionConfig == null)
                {
                    GlobalVar.motionConfig = new MotionConfig();
                }
                #region 待镭雕位
               

              
              
                #endregion
                #region 撕膜
                if (GlobalVar.motionConfig.Cut_pos == null)
                {
                    GlobalVar.motionConfig.Cut_pos = new List<CutPostion>();
                    var posCutCount = Enum.GetNames(typeof(e_PosCut)).Length;
                    for (int p = 0; p < posCutCount; p++)
                    {
                        GlobalVar.motionConfig.Cut_pos.Add(new CutPostion());
                    }
                }
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜等料位].X = (double)nud_cut_WaitX.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜等料位].Y = (double)nud_cut_WaitY.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜等料位].Z = (double)nud_cut_WaitZ.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜等料位].R1 = (double)nud_cut_WaitR1.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜等料位].R2 = (double)nud_cut_WaitR2.Value;

                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜水平位].X = (double)nud_cut_ProX.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜水平位].Y = (double)nud_cut_ProY.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜水平位].Z = (double)nud_cut_ProZ.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜水平位].R1 = (double)nud_cut_ProR1.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜水平位].R2 = (double)nud_cut_ProR2.Value;

                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.夹膜位].X = (double)nud_cut_ClampX.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.夹膜位].Y = (double)nud_cut_ClampY.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.夹膜位].Z = (double)nud_cut_ClampZ.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.夹膜位].R1 = (double)nud_cut_ClampR1.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.夹膜位].R2 = (double)nud_cut_ClampR2.Value;

                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜过度位].X = (double)nud_cut_mediumX.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜过度位].Y = (double)nud_cut_mediumY.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜过度位].Z = (double)nud_cut_mediumZ.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜过度位].R1 = (double)nud_cut_mediumR1.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜过度位].R2 = (double)nud_cut_mediumR2.Value;

                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜终点位].X = (double)nud_cut_EndX.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜终点位].Y = (double)nud_cut_EndY.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜终点位].Z = (double)nud_cut_EndZ.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜终点位].R1 = (double)nud_cut_EndR1.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜终点位].R2 = (double)nud_cut_EndR2.Value;

                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜垃圾位].X = (double)nud_cut_NGX.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜垃圾位].Y = (double)nud_cut_NGY.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜垃圾位].Z = (double)nud_cut_NGZ.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜垃圾位].R1 = (double)nud_cut_NGR1.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜垃圾位].R2 = (double)nud_cut_NGR2.Value;


                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜安全位].X = (double)nud_cut_SafeX.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜安全位].Y = (double)nud_cut_SafeY.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜安全位].Z = (double)nud_cut_SafeZ.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜安全位].R1 = (double)nud_cut_SafeR1.Value;
                GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜安全位].R2 = (double)nud_cut_SafeR2.Value;

                GlobalVar.motionConfig.ProZ_down = (double)nud_downProZ.Value;
                GlobalVar.motionConfig.ProZ_up = (double)nud_upProZ.Value;
                GlobalVar.motionConfig.clamp_Delay = (double)nud_clamp_delay.Value;
                GlobalVar.motionConfig.airCutTime = (int)nud_airCutTime.Value;

                GlobalVar.motionConfig.CutNG_HeightZ = (double)nud_cut_NGZHeight.Value;
                GlobalVar.motionConfig.CutNG_LowZ = (double)nud_cut_NGZLow.Value;
                GlobalVar.motionConfig.CutNG_time = (int)nud_cut_NGZTime.Value;
                GlobalVar.motionConfig.CutNG_Delay = (int)nud_cut_NGZDelay.Value;
                #endregion
                #region 吸放标

                if (rbn_plate1.Checked)
                {
                    GlobalVar.motionConfig.Plate1_pos = GetPos();
                }
                else
                {
                    GlobalVar.motionConfig.Plate2_pos = GetPos();
                }
                if (GlobalVar.motionConfig.LaserNG_pos == null)
                {
                    GlobalVar.motionConfig.LaserNG_pos = new List<NGPostion>();
                    for (int p = 0; p < posNGCount; p++)
                    {
                        GlobalVar.motionConfig.LaserNG_pos.Add(new NGPostion());
                    }
                }
                GlobalVar.motionConfig.LaserNG_pos[(int)e_PosNG.NG取工装位].Y = (double)nud_NGGetY.Value;
                GlobalVar.motionConfig.LaserNG_pos[(int)e_PosNG.NG取工装位].Zup = (double)nud_NGGetZup.Value;
                GlobalVar.motionConfig.LaserNG_pos[(int)e_PosNG.NG取工装位].Zdown = (double)nud_NGGetZdown.Value;

                GlobalVar.motionConfig.LaserNG_pos[(int)e_PosNG.NG放工装位].Y = (double)nud_NGPutY.Value;
                GlobalVar.motionConfig.LaserNG_pos[(int)e_PosNG.NG放工装位].Zup = (double)nud_NGPutZup.Value;
                GlobalVar.motionConfig.LaserNG_pos[(int)e_PosNG.NG放工装位].Zdown = (double)nud_NGPutZdown.Value;
    
                GlobalVar.motionConfig.scanSafeX1 = (double)nud_scanSafeX1.Value;
                GlobalVar.motionConfig.scanSafeX2 = (double)nud_scanSafeX2.Value;
            
                GlobalVar.motionConfig.NGlabelZ = (double)nud_NGLabelZ.Value;
                GlobalVar.motionConfig.checkPosY = (double)nud_checkPosY.Value;
                #endregion
                #region 飞达
                GlobalVar.motionConfig.delayFlyStop1 = (int)nud_delayStopFly1.Value;
                GlobalVar.motionConfig.delayFlyStop2 = (int)nud_delayStopFly2.Value;
                GlobalVar.motionConfig.Fly1TimeOutAlarm = (int)nud_Fly1TimeOutAlarm.Value;
                GlobalVar.motionConfig.Fly2TimeOutAlarm = (int)nud_Fly2TimeOutAlarm.Value;
                GlobalVar.motionConfig.FlyStop1 = (int)nud_flyStop1.Value;
                GlobalVar.motionConfig.FlyStop2 = (int)nud_flyStop2.Value;

                GlobalVar.motionConfig.fly1_AlarmCount = (int)nud_flyAlarmCount1.Value;
                GlobalVar.motionConfig.fly2_AlarmCount = (int)nud_flyAlarmCount2.Value;
                GlobalVar.motionConfig.fly1_Count = (int)nud_flyCount1.Value;
                GlobalVar.motionConfig.fly2_Count = (int)nud_flyCount2.Value;
                GlobalVar.motionConfig.flydelayCheck = (int)nud_flydelayCheck.Value;
                #endregion
                #region 图像处理
                #region 模板定位
                if (GlobalVar.motionConfig.visionBase1 == null)
                    GlobalVar.motionConfig.visionBase1 = new VisionPos();
                if (GlobalVar.motionConfig.visionBase2 == null)
                    GlobalVar.motionConfig.visionBase2 = new VisionPos();
                if (GlobalVar.motionConfig.TC_list == null)
                {
                    GlobalVar.motionConfig.TC_list = new List<TemplateParam>();
                }
                var tcount1 = Enum.GetNames(typeof(e_CameraPlate)).Length;
                for (int i = GlobalVar.motionConfig.TC_list.Count; i < tcount1; i++)
                {
                    GlobalVar.motionConfig.TC_list.Add(new TemplateParam());
                }
                if (GlobalVar.motionConfig.TF_list == null)
                {
                    GlobalVar.motionConfig.TF_list = new List<TemplateFindParam>();
                }
                if (GlobalVar.motionConfig.TF_list.Count > tcount1)
                {
                    GlobalVar.motionConfig.TF_list = GlobalVar.motionConfig.TF_list.Take(tcount1).ToList();
                }

                var tcount2 = Enum.GetNames(typeof(e_CameraPlate)).Length;
                for (int i = GlobalVar.motionConfig.TF_list.Count; i < tcount2; i++)
                {
                    GlobalVar.motionConfig.TF_list.Add(new TemplateFindParam());
                }
                if(GlobalVar.motionConfig.TF_list.Count>tcount2)
                {
                    GlobalVar.motionConfig.TF_list= GlobalVar.motionConfig.TF_list.Take(tcount2).ToList();
                }
                var curSelect = GetSelect_T();
                if (curSelect == e_CameraPlate.定位相机平台1)
                {
                    GlobalVar.motionConfig.visionCenter1 = Get_VisionCenter();
                    GlobalVar.motionConfig.visionPutOK1 = Get_VisionPut();
                    GlobalVar.motionConfig.visionBase1 = Get_VisionBase();
                }
                else if (curSelect == e_CameraPlate.定位相机平台2)
                {
                    GlobalVar.motionConfig.visionCenter2 = Get_VisionCenter();
                    GlobalVar.motionConfig.visionBase2 = Get_VisionBase();
                    GlobalVar.motionConfig.visionPutOK2 = Get_VisionPut();
                }
                #endregion
                #region 直线定位
                if (GlobalVar.motionConfig.PosLindFind1 == null)
                {
                    GlobalVar.motionConfig.PosLindFind1 = new List<LindFindParam>();
                }
                if (GlobalVar.motionConfig.PosLindFind2 == null)
                {
                    GlobalVar.motionConfig.PosLindFind2 = new List<LindFindParam>();
                }
              
                var LCount = (int)e_Region.直线区域2 - (int)e_Region.直线区域1 + 1;
                for (int i = GlobalVar.motionConfig.PosLindFind1.Count; i < LCount; i++)
                {
                    GlobalVar.motionConfig.PosLindFind1.Add(new LindFindParam());
                }
                for (int i = GlobalVar.motionConfig.PosLindFind2.Count; i < LCount; i++)
                {
                    GlobalVar.motionConfig.PosLindFind2.Add(new LindFindParam());
                }
              
                if (cbx_Region.SelectedIndex >= (int)e_Region.直线区域1 && cbx_Region.SelectedIndex <= (int)e_Region.直线区域2)
                {
                    if (curSelect == e_CameraPlate.定位相机平台1)
                    {
                        GlobalVar.motionConfig.PosLindFind1[cbx_Region.SelectedIndex - (int)e_Region.直线区域1] = Get_LindFindParam(GlobalVar.motionConfig.PosLindFind1[cbx_Region.SelectedIndex - (int)e_Region.直线区域1]);
                    }
                    else if (curSelect == e_CameraPlate.定位相机平台2)
                    {
                        GlobalVar.motionConfig.PosLindFind2[cbx_Region.SelectedIndex - (int)e_Region.直线区域1] = Get_LindFindParam(GlobalVar.motionConfig.PosLindFind2[cbx_Region.SelectedIndex - (int)e_Region.直线区域1]);
                    }
                }
                #endregion
                #region 圆定位
                if (GlobalVar.motionConfig.PosCircleFindCheck1 == null)
                {
                    GlobalVar.motionConfig.PosCircleFindCheck1 = new List<CircleFindParam>();
                }
                if (GlobalVar.motionConfig.PosCircleFindCheck2 == null)
                {
                    GlobalVar.motionConfig.PosCircleFindCheck2 = new List<CircleFindParam>();
                }
                var CCount =  1;
                for (int i = GlobalVar.motionConfig.PosCircleFindCheck1.Count; i < CCount; i++)
                {
                    GlobalVar.motionConfig.PosCircleFindCheck1.Add(new CircleFindParam());
                }
                for (int i = GlobalVar.motionConfig.PosCircleFindCheck2.Count; i < CCount; i++)
                {
                    GlobalVar.motionConfig.PosCircleFindCheck2.Add(new CircleFindParam());
                }
                if (cbx_Region.SelectedIndex >= (int)e_Region.抓圆区域)
                {
                    if (curSelect == e_CameraPlate.定位相机平台1)
                    {
                        GlobalVar.motionConfig.PosCircleFindCheck1[cbx_Region.SelectedIndex - (int)e_Region.抓圆区域] = Get_CircleFindParam(GlobalVar.motionConfig.PosCircleFindCheck1[cbx_Region.SelectedIndex - (int)e_Region.抓圆区域]);
                    }
                    else if (curSelect == e_CameraPlate.定位相机平台2)
                    {
                        GlobalVar.motionConfig.PosCircleFindCheck2[cbx_Region.SelectedIndex - (int)e_Region.抓圆区域] = Get_CircleFindParam(GlobalVar.motionConfig.PosCircleFindCheck2[cbx_Region.SelectedIndex - (int)e_Region.抓圆区域]);
                    }
                }
                #endregion
                GlobalVar.motionConfig.TF_list[(int)curSelect] = Get_TemplateFind();
                GlobalVar.motionConfig.TC_list[(int)curSelect] = Get_TemplateCreate();
                #region 图案
                if (curSelect==e_CameraPlate.图案相机平台1)
                {
                       GlobalVar.motionConfig.draw1 = GetDrawInfo();
                }
                else if (curSelect == e_CameraPlate.图案相机平台2)
                {
                        GlobalVar.motionConfig.draw2 = GetDrawInfo();
                }
                #endregion
                if (rbn_m_Plate1.Checked)
                {
                    if (cbx_m_RegionSelect.SelectedIndex >=(int)e_RegionCheck.检测区域1)
                    {
                        GlobalVar.motionConfig.vision_M1_list[cbx_m_RegionSelect.SelectedIndex] = Get_Measure();
                    }
                    #region  直线检测
                    GlobalVar.motionConfig.cutParma1 = Get_Cut();
                    #endregion
                    #region 圆检测
                    if (GlobalVar.motionConfig.CircleFindCheck1 == null)
                        GlobalVar.motionConfig.CircleFindCheck1 = new List<CircleFindParam>();
                    var c_arr = Enum.GetNames(typeof(e_Circle));
                    for (int n = GlobalVar.motionConfig.CircleFindCheck1.Count; n < c_arr.Length; n++)
                    {
                        GlobalVar.motionConfig.CircleFindCheck1.Add(new CircleFindParam());
                    }
                    if (cbx_m_RegionSelect.SelectedIndex >= (int)e_RegionCheck.找圆区域1 && cbx_m_RegionSelect.SelectedIndex <= (int)e_RegionCheck.找圆区域2)
                    {
                        GlobalVar.motionConfig.CircleFindCheck1[cbx_m_RegionSelect.SelectedIndex - (int)e_RegionCheck.找圆区域1] = Get_CircleParam(GlobalVar.motionConfig.CircleFindCheck1[cbx_m_RegionSelect.SelectedIndex - (int)e_RegionCheck.找圆区域1]);
                    }
                    #endregion
                }
                else
                {
                    if (cbx_m_RegionSelect.SelectedIndex >= (int)e_RegionCheck.检测区域1)
                    {
                        GlobalVar.motionConfig.vision_M2_list[cbx_m_RegionSelect.SelectedIndex] = Get_Measure();
                    }
                    #region 直线检测
                    GlobalVar.motionConfig.cutParma2 = Get_Cut();
               
                    #endregion
                    #region 圆检测
                    if (GlobalVar.motionConfig.CircleFindCheck2 == null)
                        GlobalVar.motionConfig.CircleFindCheck2= new List<CircleFindParam>();
                    var c_arr = Enum.GetNames(typeof(e_Circle));
                    for (int n = GlobalVar.motionConfig.CircleFindCheck2.Count; n < c_arr.Length; n++)
                    {
                        GlobalVar.motionConfig.CircleFindCheck2.Add(new CircleFindParam());
                    }
                    if (cbx_m_RegionSelect.SelectedIndex >= (int)e_RegionCheck.找圆区域1 && cbx_m_RegionSelect.SelectedIndex <= (int)e_RegionCheck.找圆区域2)
                    {
                        GlobalVar.motionConfig.CircleFindCheck2[cbx_m_RegionSelect.SelectedIndex - (int)e_RegionCheck.找圆区域1] = Get_CircleParam(GlobalVar.motionConfig.CircleFindCheck2[cbx_m_RegionSelect.SelectedIndex - (int)e_RegionCheck.找圆区域1]);
                    }
                    #endregion
                }
                #region 检测基准
                if (GlobalVar.motionConfig.posAfterLabel == null)
                    GlobalVar.motionConfig.posAfterLabel = new VisionPostion();
                if (rbn_m_Plate1.Checked)
                {
                    GlobalVar.motionConfig.posAfterLabel.areaMax1 = (double)nud_p_AreaMax.Value;
                    GlobalVar.motionConfig.posAfterLabel.areaMin1 = (double)nud_p_AreaMin.Value;
                    GlobalVar.motionConfig.posAfterLabel.grayMax1 = (double)nud_p_grayMax.Value;
                    GlobalVar.motionConfig.posAfterLabel.grayMin1 = (double)nud_p_grayMin.Value;
                }
                else
                {
                    GlobalVar.motionConfig.posAfterLabel.areaMax2 = (double)nud_p_AreaMax.Value;
                    GlobalVar.motionConfig.posAfterLabel.areaMin2 = (double)nud_p_AreaMin.Value;
                    GlobalVar.motionConfig.posAfterLabel.grayMax2 = (double)nud_p_grayMax.Value;
                    GlobalVar.motionConfig.posAfterLabel.grayMin2 = (double)nud_p_grayMin.Value;
                }
                

                GlobalVar.motionConfig.posAfterLabel.pixXReal = (double)nud_p_pixXreal.Value;
                GlobalVar.motionConfig.posAfterLabel.pixYReal = (double)nud_p_pixYreal.Value;
                GlobalVar.motionConfig.posAfterLabel.RealX1 = (double)nud_p_realX1.Value;
                GlobalVar.motionConfig.posAfterLabel.RealY1 = (double)nud_p_realY1.Value;
                GlobalVar.motionConfig.posAfterLabel.RealR1 = (double)nud_p_realR1.Value;
                GlobalVar.motionConfig.posAfterLabel.RealX2 = (double)nud_p_realX2.Value;
                GlobalVar.motionConfig.posAfterLabel.RealY2 = (double)nud_p_realY2.Value;
                GlobalVar.motionConfig.posAfterLabel.RealR2 = (double)nud_p_realR2.Value;
                #endregion
                GlobalVar.motionConfig.IsEnableLinePos = ckb_IsEnableLine.Checked;
                GlobalVar.motionConfig.IsEnableCircleCheck = ckb_isCircleCheck.Checked;
                #endregion
                #region 读卡器
                if (GlobalVar.motionConfig.boxReadInfo == null)
                    GlobalVar.motionConfig.boxReadInfo = new BoxReadInfo();
                GlobalVar.motionConfig.boxReadInfo.dataLength = (int)nud_r_dataLength.Value;
                GlobalVar.motionConfig.boxReadInfo.NGNG = tbx_r_NGNG.Text;
                GlobalVar.motionConfig.boxReadInfo.MR = tbx_r_RM.Text;
                GlobalVar.motionConfig.boxReadInfo.station = (int)nud_r_station.Value;
                GlobalVar.motionConfig.boxReadInfo.StartAddress1 = (int)nud_r_StartAddress1.Value;
                GlobalVar.motionConfig.boxReadInfo.StartAddress2 = (int)nud_r_StartAddress2.Value;
                GlobalVar.motionConfig.boxReadInfo.NGOK = tbx_r_NGOK.Text;
                GlobalVar.motionConfig.boxReadInfo.OK = tbx_r_OK.Text;
                GlobalVar.motionConfig.boxReadInfo.IsReadWriteOK = ckb_isReadWriteOK.Checked;
                GlobalVar.motionConfig.boxReadInfo.IsEnable = ckb_IsEnableR.Checked;
                GlobalVar.motionConfig.boxReadInfo.nTimes = (int)nud_r_ntimes.Value;
                GlobalVar.motionConfig.boxReadInfo.NGflag = tbx_r_NGflag.Text.Trim();
                GlobalVar.motionConfig.boxReadInfo.NumberStart = (int)nud_r_NumberStart.Value;  
                GlobalVar.motionConfig.boxReadInfo.NumberEnd = (int)nud_r_NumberEnd.Value;
                GlobalVar.motionConfig.boxReadInfo.IsCheckNumber = ckb_r_IsNumber.Checked;
                GlobalVar.motionConfig.boxReadInfo.minLength =(int) nud_r_dataMinLength.Value;

                GlobalVar.motionConfig.boxReadInfo.have_string = tbx_r_havestring.Text;
                GlobalVar.motionConfig.boxReadInfo.have_P = tbx_r_p.Text;
                GlobalVar.motionConfig.boxReadInfo.have_O = tbx_r_O.Text;
                GlobalVar.motionConfig.boxReadInfo.have_OPcount = (int)nud_r_havecount.Value;
                GlobalVar.motionConfig.boxReadInfo.IsCheckInclude = ckb_IsCheckInclude.Checked;
                GlobalVar.motionConfig.boxReadInfo.have_R = tbx_r_R.Text;
                #endregion
              
            }
            catch (Exception ex)
            {
                displalyInfo.displayInfoMain?.Invoke($"ControlToParameter error : {ex}");
            }
        }
        
        private List<AbsPostion> GetPos()
        {
            var posList = new List<AbsPostion>();
            for (int i = 0; i < posCount; i++)
            {
                posList.Add(new AbsPostion());
            }
            posList[(int)e_Pos.拍照位].X = (double)nud_pos_scanX.Value;
            posList[(int)e_Pos.拍照位].Y = (double)nud_pos_scanY.Value;
            posList[(int)e_Pos.拍照位].Z = (double)nud_pos_scanZ.Value;
            posList[(int)e_Pos.拍照位].R = (double)nud_pos_scanR.Value;

            posList[(int)e_Pos.拍照检测位].X = (double)nud_pos_ResultX.Value;
            posList[(int)e_Pos.拍照检测位].Y = (double)nud_pos_ResultY.Value;
            posList[(int)e_Pos.拍照检测位].Z = (double)nud_pos_ResultZ.Value;
            posList[(int)e_Pos.拍照检测位].R = (double)nud_pos_ResultR.Value;

            posList[(int)e_Pos.NG放标位].X = (double)nud_pos_NGX.Value;
            posList[(int)e_Pos.NG放标位].Y = (double)nud_pos_NGY.Value;
            posList[(int)e_Pos.NG放标位].Z = (double)nud_pos_NGZ.Value;
            posList[(int)e_Pos.NG放标位].ZHeight = (double)nud_pos_NGZHeight.Value;
            posList[(int)e_Pos.NG放标位].R = (double)nud_pos_NGR.Value;

            posList[(int)e_Pos.OK放标位].X = (double)nud_pos_OKX.Value;
            posList[(int)e_Pos.OK放标位].Y = (double)nud_pos_OKY.Value;
            posList[(int)e_Pos.OK放标位].Z = (double)nud_pos_OKZ.Value;
            posList[(int)e_Pos.OK放标位].ZHeight = (double)nud_pos_OKZHeight.Value;
            posList[(int)e_Pos.OK放标位].R = (double)nud_pos_OKR.Value;

            posList[(int)e_Pos.飞达取标位].X = (double)nud_GetLabelX.Value;
            posList[(int)e_Pos.飞达取标位].Y = (double)nud_GetLabelY.Value;
            posList[(int)e_Pos.飞达取标位].Z = (double)nud_GetLabelZ.Value;
            posList[(int)e_Pos.飞达取标位].ZHeight = (double)nud_GetLabelZHeight.Value;
            posList[(int)e_Pos.飞达取标位].R = (double)nud_GetLabelR.Value;
            return posList;
        }
        private void SetPosList(List<AbsPostion> posList)
        {
            if (posList == null)
            {
                posList = new List<AbsPostion>();
                for (int i = 0; i < posCount; i++)
                {
                    posList.Add(new AbsPostion());
                }
            }
            if (posList[(int)e_Pos.拍照位].X < (double)nud_pos_scanX.Minimum || posList[(int)e_Pos.拍照位].X > (double)nud_pos_scanX.Maximum)
                posList[(int)e_Pos.拍照位].X = (double)nud_pos_scanX.Maximum;
            if (posList[(int)e_Pos.拍照位].Y < (double)nud_pos_scanY.Minimum || posList[(int)e_Pos.拍照位].Y > (double)nud_pos_scanY.Maximum)
                posList[(int)e_Pos.拍照位].Y = (double)nud_pos_scanY.Minimum;
            if (posList[(int)e_Pos.拍照位].Z < (double)nud_pos_scanZ.Minimum || posList[(int)e_Pos.拍照位].Z > (double)nud_pos_scanZ.Maximum)
                posList[(int)e_Pos.拍照位].Z = (double)nud_pos_scanZ.Minimum;

            nud_pos_scanX.Value = (decimal)posList[(int)e_Pos.拍照位].X;
            nud_pos_scanY.Value = (decimal)posList[(int)e_Pos.拍照位].Y;
            nud_pos_scanZ.Value = (decimal)posList[(int)e_Pos.拍照位].Z;
            nud_pos_scanR.Value = (decimal)posList[(int)e_Pos.拍照位].R;

            if (posList[(int)e_Pos.拍照检测位].Y < (double)nud_pos_ResultY.Minimum || posList[(int)e_Pos.拍照检测位].Y> (double)nud_pos_ResultY.Maximum)
                posList[(int)e_Pos.拍照检测位].Y = (double)nud_pos_ResultY.Minimum;
       
            nud_pos_ResultY.Value = (decimal)posList[(int)e_Pos.拍照检测位].Y;

            if (posList[(int)e_Pos.NG放标位].X < (double)nud_pos_NGX.Minimum || posList[(int)e_Pos.NG放标位].X > (double)nud_pos_NGX.Maximum)
                posList[(int)e_Pos.NG放标位].X = (double)nud_pos_NGX.Maximum;
            if (posList[(int)e_Pos.NG放标位].Y < (double)nud_pos_NGY.Minimum || posList[(int)e_Pos.NG放标位].Y > (double)nud_pos_NGY.Maximum)
                posList[(int)e_Pos.NG放标位].Y = (double)nud_pos_NGY.Minimum;
            if (posList[(int)e_Pos.NG放标位].Z < (double)nud_pos_NGZ.Minimum || posList[(int)e_Pos.NG放标位].Z > (double)nud_pos_NGZ.Maximum)
                posList[(int)e_Pos.NG放标位].Z = (double)nud_pos_NGZ.Minimum;
            if (posList[(int)e_Pos.NG放标位].ZHeight < (double)nud_pos_NGZHeight.Minimum || posList[(int)e_Pos.NG放标位].ZHeight > (double)nud_pos_NGZHeight.Maximum)
                posList[(int)e_Pos.NG放标位].ZHeight = (double)nud_pos_NGZHeight.Minimum;
            if (posList[(int)e_Pos.NG放标位].R < (double)nud_pos_NGR.Minimum || posList[(int)e_Pos.NG放标位].R > (double)nud_pos_NGR.Maximum)
                posList[(int)e_Pos.NG放标位].R = (double)nud_pos_NGR.Minimum;
            nud_pos_NGX.Value = (decimal)posList[(int)e_Pos.NG放标位].X;
            nud_pos_NGY.Value = (decimal)posList[(int)e_Pos.NG放标位].Y;
            nud_pos_NGZ.Value = (decimal)posList[(int)e_Pos.NG放标位].Z;
            nud_pos_NGZHeight.Value = (decimal)posList[(int)e_Pos.NG放标位].ZHeight;
            nud_pos_NGR.Value = (decimal)posList[(int)e_Pos.NG放标位].R;

            if (posList[(int)e_Pos.OK放标位].X < (double)nud_pos_OKX.Minimum || posList[(int)e_Pos.OK放标位].X > (double)nud_pos_OKX.Maximum)
                posList[(int)e_Pos.OK放标位].X = (double)nud_pos_OKX.Maximum;
            if (posList[(int)e_Pos.OK放标位].Y < (double)nud_pos_OKY.Minimum || posList[(int)e_Pos.OK放标位].Y > (double)nud_pos_OKY.Maximum)
                posList[(int)e_Pos.OK放标位].Y = (double)nud_pos_OKY.Minimum;
            if (posList[(int)e_Pos.OK放标位].Z < (double)nud_pos_OKZ.Minimum || posList[(int)e_Pos.OK放标位].Z > (double)nud_pos_OKZ.Maximum)
                posList[(int)e_Pos.OK放标位].Z = (double)nud_pos_OKZ.Minimum;
            if (posList[(int)e_Pos.OK放标位].ZHeight < (double)nud_pos_OKZHeight.Minimum || posList[(int)e_Pos.OK放标位].ZHeight > (double)nud_pos_OKZHeight.Maximum)
                posList[(int)e_Pos.OK放标位].ZHeight = (double)nud_pos_OKZHeight.Minimum;
            if (posList[(int)e_Pos.OK放标位].R < (double)nud_pos_OKR.Minimum || posList[(int)e_Pos.OK放标位].R > (double)nud_pos_OKR.Maximum)
                posList[(int)e_Pos.OK放标位].R = (double)nud_pos_OKR.Minimum;
            nud_pos_OKX.Value = (decimal)posList[(int)e_Pos.OK放标位].X;
            nud_pos_OKY.Value = (decimal)posList[(int)e_Pos.OK放标位].Y;
            nud_pos_OKZ.Value = (decimal)posList[(int)e_Pos.OK放标位].Z;
            nud_pos_OKZHeight.Value = (decimal)posList[(int)e_Pos.OK放标位].ZHeight;
            nud_pos_OKR.Value = (decimal)posList[(int)e_Pos.OK放标位].R;


            if (posList[(int)e_Pos.飞达取标位].X < (double)nud_GetLabelX.Minimum || posList[(int)e_Pos.飞达取标位].X > (double)nud_GetLabelX.Maximum)
                posList[(int)e_Pos.飞达取标位].X = (double)nud_GetLabelX.Maximum;
            if (posList[(int)e_Pos.飞达取标位].Y < (double)nud_GetLabelY.Minimum || posList[(int)e_Pos.飞达取标位].Y > (double)nud_GetLabelY.Maximum)
                posList[(int)e_Pos.飞达取标位].Y = (double)nud_GetLabelY.Minimum;
            if (posList[(int)e_Pos.飞达取标位].Z < (double)nud_GetLabelZ.Minimum || posList[(int)e_Pos.飞达取标位].Z > (double)nud_GetLabelZ.Maximum)
                posList[(int)e_Pos.飞达取标位].Z = (double)nud_GetLabelZ.Minimum;
            if (posList[(int)e_Pos.飞达取标位].ZHeight < (double)nud_GetLabelZHeight.Minimum || posList[(int)e_Pos.飞达取标位].ZHeight > (double)nud_GetLabelZHeight.Maximum)
                posList[(int)e_Pos.飞达取标位].ZHeight = (double)nud_GetLabelZHeight.Minimum;
            if (posList[(int)e_Pos.飞达取标位].R < (double)nud_GetLabelR.Minimum || posList[(int)e_Pos.飞达取标位].R > (double)nud_GetLabelR.Maximum)
                posList[(int)e_Pos.飞达取标位].R = (double)nud_GetLabelR.Minimum;

            nud_GetLabelX.Value = (decimal)posList[(int)e_Pos.飞达取标位].X;
            nud_GetLabelY.Value = (decimal)posList[(int)e_Pos.飞达取标位].Y;
            nud_GetLabelZ.Value = (decimal)posList[(int)e_Pos.飞达取标位].Z;
            nud_GetLabelZHeight.Value = (decimal)posList[(int)e_Pos.飞达取标位].ZHeight;
            nud_GetLabelR.Value = (decimal)posList[(int)e_Pos.飞达取标位].R;

        }
      
        private e_CameraPlate GetSelect_T()
        {
            if (rbn_v_plate1.Checked && rbn_pos.Checked)
                return e_CameraPlate.定位相机平台1;
            else if (rbn_v_plate2.Checked && rbn_pos.Checked)
                return e_CameraPlate.定位相机平台2;
            else if (rbn_v_plate1.Checked && rbn_scan.Checked)
                return e_CameraPlate.图案相机平台1;
            else if (rbn_v_plate2.Checked && rbn_scan.Checked)
                return e_CameraPlate.图案相机平台2;
            else if (rbn_v_plate1.Checked && rbn_check.Checked)
                return e_CameraPlate.检测相机平台1;
            else if (rbn_v_plate2.Checked && rbn_check.Checked)
                return e_CameraPlate.检测相机平台2;
            return e_CameraPlate.定位相机平台1;
        }
        private e_CameraPlate GetSelect_M()
        {
            
            if (rbn_m_Plate1.Checked )
                return e_CameraPlate.检测相机平台1;
            else
                return e_CameraPlate.检测相机平台2;
   
        }
       
        /// <summary>
        /// 配置显示到控件上
        /// </summary>
        private void ParameterToControl()
        {
            try
            {
                if (GlobalVar.motionConfig == null)
                {
                    GlobalVar.motionConfig = new MotionConfig();
                }
              
         
                #region 撕膜
                if (GlobalVar.motionConfig.Cut_pos == null)
                {
                    GlobalVar.motionConfig.Cut_pos = new List<CutPostion>();
                    var posCutCount = Enum.GetNames(typeof(e_PosCut)).Length;
                    for (int p = 0; p < posCutCount; p++)
                    {
                        GlobalVar.motionConfig.Cut_pos.Add(new CutPostion());
                    }
                }
                nud_cut_WaitX.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜等料位].X;
                nud_cut_WaitY.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜等料位].Y;
                nud_cut_WaitZ.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜等料位].Z;
                nud_cut_WaitR1.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜等料位].R1;
                nud_cut_WaitR2.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜等料位].R2;

                nud_cut_ProX.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜水平位].X;
                nud_cut_ProY.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜水平位].Y;
                nud_cut_ProZ.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜水平位].Z;
                nud_cut_ProR1.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜水平位].R1;
                nud_cut_ProR2.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜水平位].R2;

                nud_cut_ClampX.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.夹膜位].X;
                nud_cut_ClampY.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.夹膜位].Y;
                nud_cut_ClampZ.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.夹膜位].Z;
                nud_cut_ClampR1.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.夹膜位].R1;
                nud_cut_ClampR2.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.夹膜位].R2;

                nud_cut_EndX.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜终点位].X;
                nud_cut_EndY.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜终点位].Y;
                nud_cut_EndZ.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜终点位].Z;
                nud_cut_EndR1.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜终点位].R1;
                nud_cut_EndR2.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜终点位].R2;

                nud_cut_mediumX.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜过度位].X;
                nud_cut_mediumY.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜过度位].Y;
                nud_cut_mediumZ.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜过度位].Z;
                nud_cut_mediumR1.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜过度位].R1;
                nud_cut_mediumR2.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜过度位].R2;

                nud_cut_NGX.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜垃圾位].X;
                nud_cut_NGY.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜垃圾位].Y;
                nud_cut_NGZ.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜垃圾位].Z;
                nud_cut_NGR1.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜垃圾位].R1;
                nud_cut_NGR2.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜垃圾位].R2;

                nud_cut_SafeX.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜安全位].X;
                nud_cut_SafeY.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜安全位].Y;
                nud_cut_SafeZ.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜安全位].Z;
                nud_cut_SafeR1.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜安全位].R1;
                nud_cut_SafeR2.Value = (decimal)GlobalVar.motionConfig.Cut_pos[(int)e_PosCut.撕膜安全位].R2;

                nud_upProZ.Value = (decimal)GlobalVar.motionConfig.ProZ_up;
                nud_downProZ.Value = (decimal)GlobalVar.motionConfig.ProZ_down;

               nud_clamp_delay.Value = (decimal)GlobalVar.motionConfig.clamp_Delay;


                nud_cut_NGZHeight.Value = (decimal)GlobalVar.motionConfig.CutNG_HeightZ;
                nud_cut_NGZLow.Value = (decimal)GlobalVar.motionConfig.CutNG_LowZ;
                nud_cut_NGZTime.Value = (decimal)GlobalVar.motionConfig.CutNG_time;
                nud_cut_NGZDelay.Value = (decimal)GlobalVar.motionConfig.CutNG_Delay;

                nud_airCutTime.Value = (decimal)GlobalVar.motionConfig.airCutTime;
                #endregion
                #region 吸放标
                if (rbn_plate1.Checked)
                {
                    SetLabelLimit((int)e_platePro.平台1);
                    SetPosList(GlobalVar.motionConfig.Plate1_pos);
                    
                }
                else
                {
                    SetLabelLimit((int)e_platePro.平台2);
                    SetPosList(GlobalVar.motionConfig.Plate2_pos);
                    
                }
                if (GlobalVar.motionConfig.LaserNG_pos == null)
                {
                    GlobalVar.motionConfig.LaserNG_pos = new List<NGPostion>();
                    for (int p = 0; p < posNGCount; p++)
                    {
                        GlobalVar.motionConfig.LaserNG_pos.Add(new NGPostion());
                    }
                }
                nud_NGGetY.Value = (decimal)GlobalVar.motionConfig.LaserNG_pos[(int)e_PosNG.NG取工装位].Y;
                nud_NGGetZup.Value = (decimal)GlobalVar.motionConfig.LaserNG_pos[(int)e_PosNG.NG取工装位].Zup;
                nud_NGGetZdown.Value = (decimal)GlobalVar.motionConfig.LaserNG_pos[(int)e_PosNG.NG取工装位].Zdown;
                nud_NGPutY.Value = (decimal)GlobalVar.motionConfig.LaserNG_pos[(int)e_PosNG.NG放工装位].Y;
                nud_NGPutZup.Value = (decimal)GlobalVar.motionConfig.LaserNG_pos[(int)e_PosNG.NG放工装位].Zup;
                nud_NGPutZdown.Value = (decimal)GlobalVar.motionConfig.LaserNG_pos[(int)e_PosNG.NG放工装位].Zdown;
          
                nud_scanSafeX1.Value = (decimal)GlobalVar.motionConfig.scanSafeX1;
                nud_scanSafeX2.Value = (decimal)GlobalVar.motionConfig.scanSafeX2;

                nud_NGLabelZ.Value = (decimal)GlobalVar.motionConfig.NGlabelZ;
                if(GlobalVar.motionConfig.checkPosY<(double)nud_checkPosY.Minimum|| GlobalVar.motionConfig.checkPosY > (double)nud_checkPosY.Maximum)
                {
                    GlobalVar.motionConfig.checkPosY = (double)nud_checkPosY.Minimum;
                }
                nud_checkPosY.Value = (decimal)GlobalVar.motionConfig.checkPosY;
                #endregion
                #region 飞达
                nud_delayStopFly1.Value = GlobalVar.motionConfig.delayFlyStop1;
                nud_delayStopFly2.Value = GlobalVar.motionConfig.delayFlyStop2;
                nud_Fly1TimeOutAlarm.Value = GlobalVar.motionConfig.Fly1TimeOutAlarm;
                nud_Fly2TimeOutAlarm.Value = GlobalVar.motionConfig.Fly2TimeOutAlarm;
                nud_flyStop1.Value = GlobalVar.motionConfig.FlyStop1;
                nud_flyStop2.Value = GlobalVar.motionConfig.FlyStop2;
              

                nud_flyAlarmCount1.Value = GlobalVar.motionConfig.fly1_AlarmCount;
                nud_flyAlarmCount2.Value = GlobalVar.motionConfig.fly2_AlarmCount;
                nud_flyCount1.Value = GlobalVar.motionConfig.fly1_Count;
                nud_flyCount2.Value = GlobalVar.motionConfig.fly2_Count;

                nud_flydelayCheck.Value = GlobalVar.motionConfig.flydelayCheck;
                #endregion
                #region 图像处理
                if (GlobalVar.motionConfig.visionBase1 == null)
                    GlobalVar.motionConfig.visionBase1 = new VisionPos();
                if (GlobalVar.motionConfig.visionBase2 == null)
                    GlobalVar.motionConfig.visionBase2 = new VisionPos();
                #region 定位相机
                var curSelect = GetSelect_T();
                if (GlobalVar.motionConfig.TC_list == null)
                    GlobalVar.motionConfig.TC_list = new List<TemplateParam>();
                var tcount1 = Enum.GetNames(typeof(e_CameraPlate)).Length;
                for (int i = 0; i < tcount1; i++)
                {
                    GlobalVar.motionConfig.TC_list.Add(new TemplateParam());
                }
                if (GlobalVar.motionConfig.TF_list == null)
                    GlobalVar.motionConfig.TF_list = new List<TemplateFindParam>();
                var tcount2 = Enum.GetNames(typeof(e_CameraPlate)).Length;
                for (int i = 0; i < tcount2; i++)
                {
                    GlobalVar.motionConfig.TF_list.Add(new TemplateFindParam());
                }
                set_TemplateFind(GlobalVar.motionConfig.TF_list[(int)curSelect]);
                set_TemplateCreate(GlobalVar.motionConfig.TC_list[(int)curSelect]);
                if (curSelect==e_CameraPlate.定位相机平台1)
                {
                    set_VisionCenter(GlobalVar.motionConfig.visionCenter1);
                    set_VisionPut(GlobalVar.motionConfig.visionPutOK1);
                    set_VisionBase(GlobalVar.motionConfig.visionBase1);
                }
                else if(curSelect == e_CameraPlate.定位相机平台2)
                {
                    set_VisionCenter(GlobalVar.motionConfig.visionCenter2);
                    set_VisionPut(GlobalVar.motionConfig.visionPutOK2);
                    set_VisionBase(GlobalVar.motionConfig.visionBase2);
                }
                #endregion
                #region 定位直线


                if (GlobalVar.motionConfig.PosLindFind1 == null)
                {
                    GlobalVar.motionConfig.PosLindFind1 = new List<LindFindParam>();
                }
                if (GlobalVar.motionConfig.PosLindFind2 == null)
                {
                    GlobalVar.motionConfig.PosLindFind2 = new List<LindFindParam>();
                }
                var LCount = (int)e_Region.直线区域2 - (int)e_Region.直线区域1 + 1;
                for (int i = GlobalVar.motionConfig.PosLindFind1.Count; i < LCount; i++)
                {
                    GlobalVar.motionConfig.PosLindFind1.Add(new LindFindParam());
                }
                for (int i = GlobalVar.motionConfig.PosLindFind2.Count; i < LCount; i++)
                {
                    GlobalVar.motionConfig.PosLindFind2.Add(new LindFindParam());
                }
              
                if (cbx_Region.SelectedIndex>=(int)e_Region.直线区域1&& cbx_Region.SelectedIndex<= (int)e_Region.直线区域2)
                {
                    if (curSelect == e_CameraPlate.定位相机平台1)
                    {
                        var lineParama = GlobalVar.motionConfig.PosLindFind1[cbx_Region.SelectedIndex - (int)e_Region.直线区域1];
                        Set_LindFindParam(lineParama);
                    }
                    else if (curSelect == e_CameraPlate.定位相机平台2)
                    {
                        var lineParama = GlobalVar.motionConfig.PosLindFind2[cbx_Region.SelectedIndex - (int)e_Region.直线区域1];
                        Set_LindFindParam(lineParama);
                    }
                }
                #endregion
                #region 定位圆
                if (GlobalVar.motionConfig.PosCircleFindCheck1 == null)
                {
                    GlobalVar.motionConfig.PosCircleFindCheck1 = new List<CircleFindParam>();
                }
                if (GlobalVar.motionConfig.PosCircleFindCheck2 == null)
                {
                    GlobalVar.motionConfig.PosCircleFindCheck2 = new List<CircleFindParam>();
                }
                var cCount =1;
                for (int i = GlobalVar.motionConfig.PosCircleFindCheck1.Count; i < cCount; i++)
                {
                    GlobalVar.motionConfig.PosCircleFindCheck1.Add(new CircleFindParam());
                }
                for (int i = GlobalVar.motionConfig.PosCircleFindCheck2.Count; i < LCount; i++)
                {
                    GlobalVar.motionConfig.PosCircleFindCheck2.Add(new CircleFindParam());
                }
                
                if (cbx_Region.SelectedIndex >= (int)e_Region.抓圆区域)
                {
                    if (curSelect == e_CameraPlate.定位相机平台1)
                    {
                        var circleParama = GlobalVar.motionConfig.PosCircleFindCheck1[cbx_Region.SelectedIndex - (int)e_Region.抓圆区域];
                        Set_CircleFindParam(circleParama);
                    }
                    else if (curSelect == e_CameraPlate.定位相机平台2)
                    {
                        var circleParama = GlobalVar.motionConfig.PosCircleFindCheck2[cbx_Region.SelectedIndex - (int)e_Region.抓圆区域];
                        Set_CircleFindParam(circleParama);
                    }
                }
                #endregion
                #region  图案
                if (curSelect == e_CameraPlate.图案相机平台1)
                {
                    SetDrawInfo(GlobalVar.motionConfig.draw1);
                }
                else if (curSelect == e_CameraPlate.图案相机平台2)
                {
                    SetDrawInfo(GlobalVar.motionConfig.draw2);
                }
                #endregion
                #region 检测
                var mcount = cbx_m_RegionSelect.Items.Count;
                if (GlobalVar.motionConfig.vision_M1_list == null)
                    GlobalVar.motionConfig.vision_M1_list = new List<MeasureParma>();
                for (int i = GlobalVar.motionConfig.vision_M1_list.Count; i < mcount; i++)
                {
                    GlobalVar.motionConfig.vision_M1_list.Add(new MeasureParma());
                }
                if (GlobalVar.motionConfig.vision_M2_list == null)
                    GlobalVar.motionConfig.vision_M2_list = new List<MeasureParma>();
                for (int i = GlobalVar.motionConfig.vision_M2_list.Count; i < mcount; i++)
                {
                        GlobalVar.motionConfig.vision_M2_list.Add(new MeasureParma());
                }

                if(rbn_m_Plate1.Checked)
                {
                    #region 直线检测
                    if (cbx_m_RegionSelect.SelectedIndex>-1)
                    {
                        Set_Measure(GlobalVar.motionConfig.vision_M1_list[cbx_m_RegionSelect.SelectedIndex]);
                    }
                    if (GlobalVar.motionConfig.cutParma1 == null)
                        GlobalVar.motionConfig.cutParma1 = new CutParma();
                    Set_Cut(GlobalVar.motionConfig.cutParma1);
           
                    var l_arr = Enum.GetNames(typeof( e_Line));
                    #endregion
                    #region 圆检测
                    if (GlobalVar.motionConfig.CircleFindCheck1 == null)
                        GlobalVar.motionConfig.CircleFindCheck1 = new List<CircleFindParam>();
                    var c_arr = Enum.GetNames(typeof(e_Circle));
                    for (int n = GlobalVar.motionConfig.CircleFindCheck1.Count; n < c_arr.Length; n++)
                    {
                        GlobalVar.motionConfig.CircleFindCheck1.Add(new CircleFindParam());
                    }
                    Set_CircleParam(GlobalVar.motionConfig.CircleFindCheck1[0]);
                    #endregion
                }
                else
                {
                    #region 直线检测
                    if (cbx_m_RegionSelect.SelectedIndex > -1)
                    {
                        Set_Measure(GlobalVar.motionConfig.vision_M2_list[cbx_m_RegionSelect.SelectedIndex]);
                       
                    }
                    if (GlobalVar.motionConfig.cutParma2 == null)
                        GlobalVar.motionConfig.cutParma2 = new CutParma();
                    Set_Cut(GlobalVar.motionConfig.cutParma2);
                
                    #endregion
                    #region 圆检测
                    if (GlobalVar.motionConfig.CircleFindCheck2 == null)
                        GlobalVar.motionConfig.CircleFindCheck2 = new List<CircleFindParam>();
                    var c_arr = Enum.GetNames(typeof(e_Line));
                    for (int n = GlobalVar.motionConfig.CircleFindCheck2.Count; n < c_arr.Length; n++)
                    {
                        GlobalVar.motionConfig.CircleFindCheck2.Add(new CircleFindParam());
                    }
                    Set_CircleParam(GlobalVar.motionConfig.CircleFindCheck2[0]);
                    #endregion
                }

                if (GlobalVar.motionConfig.posAfterLabel == null)
                    GlobalVar.motionConfig.posAfterLabel = new VisionPostion();

                if (rbn_m_Plate1.Checked)
                {
                    nud_p_grayMin.Value = (decimal)GlobalVar.motionConfig.posAfterLabel.grayMin1;
                    nud_p_grayMax.Value = (decimal)GlobalVar.motionConfig.posAfterLabel.grayMax1;
                    nud_p_AreaMin.Value = (decimal)GlobalVar.motionConfig.posAfterLabel.areaMin1;
                    nud_p_AreaMax.Value = (decimal)GlobalVar.motionConfig.posAfterLabel.areaMax1;
                }
                else
                {
                    nud_p_grayMin.Value = (decimal)GlobalVar.motionConfig.posAfterLabel.grayMin2;
                    nud_p_grayMax.Value = (decimal)GlobalVar.motionConfig.posAfterLabel.grayMax2;
                    nud_p_AreaMin.Value = (decimal)GlobalVar.motionConfig.posAfterLabel.areaMin2;
                    nud_p_AreaMax.Value = (decimal)GlobalVar.motionConfig.posAfterLabel.areaMax2;
                }
                    

                nud_p_pixXreal.Value = (decimal)GlobalVar.motionConfig.posAfterLabel.pixXReal;
                nud_p_pixYreal.Value = (decimal)GlobalVar.motionConfig.posAfterLabel.pixYReal;
                nud_p_realX1.Value = (decimal)GlobalVar.motionConfig.posAfterLabel.RealX1;
                nud_p_realX2.Value = (decimal)GlobalVar.motionConfig.posAfterLabel.RealX2;
                nud_p_realY1.Value = (decimal)GlobalVar.motionConfig.posAfterLabel.RealY1;
                nud_p_realY2.Value = (decimal)GlobalVar.motionConfig.posAfterLabel.RealY2;
                nud_p_realR1.Value = (decimal)GlobalVar.motionConfig.posAfterLabel.RealR1;
                nud_p_realR2.Value = (decimal)GlobalVar.motionConfig.posAfterLabel.RealR2;
                ckb_IsEnableLine.Checked = GlobalVar.motionConfig.IsEnableLinePos;

                ckb_isCircleCheck.Checked = GlobalVar.motionConfig.IsEnableCircleCheck;
                #endregion
                #endregion
                #region 读卡器
                if (GlobalVar.motionConfig.boxReadInfo == null)
                    GlobalVar.motionConfig.boxReadInfo = new BoxReadInfo();
                tbx_r_RM.Text = GlobalVar.motionConfig.boxReadInfo.MR;
                tbx_r_NGNG.Text = GlobalVar.motionConfig.boxReadInfo.NGNG;
                tbx_r_NGOK.Text = GlobalVar.motionConfig.boxReadInfo.NGOK;
                tbx_r_OK.Text = GlobalVar.motionConfig.boxReadInfo.OK;
                nud_r_StartAddress1.Value = GlobalVar.motionConfig.boxReadInfo.StartAddress1;
                nud_r_StartAddress2.Value = GlobalVar.motionConfig.boxReadInfo.StartAddress2;
                nud_r_station.Value = GlobalVar.motionConfig.boxReadInfo.station;
                nud_r_dataLength.Value = GlobalVar.motionConfig.boxReadInfo.dataLength;
                ckb_IsEnableR.Checked = GlobalVar.motionConfig.boxReadInfo.IsEnable;
                ckb_isReadWriteOK.Checked = GlobalVar.motionConfig.boxReadInfo.IsReadWriteOK;
                nud_r_ntimes.Value = GlobalVar.motionConfig.boxReadInfo.nTimes;
                tbx_r_NGflag.Text = GlobalVar.motionConfig.boxReadInfo.NGflag;
                nud_r_NumberEnd.Value = GlobalVar.motionConfig.boxReadInfo.NumberEnd;
                nud_r_NumberStart.Value = GlobalVar.motionConfig.boxReadInfo.NumberStart;
                ckb_r_IsNumber.Checked = GlobalVar.motionConfig.boxReadInfo.IsCheckNumber;
                nud_r_dataMinLength.Value = GlobalVar.motionConfig.boxReadInfo.minLength;

                tbx_r_havestring.Text = GlobalVar.motionConfig.boxReadInfo.have_string;
                tbx_r_p.Text = GlobalVar.motionConfig.boxReadInfo.have_P;
                tbx_r_O.Text = GlobalVar.motionConfig.boxReadInfo.have_O;
                tbx_r_R.Text = GlobalVar.motionConfig.boxReadInfo.have_R;
                nud_r_havecount.Value = GlobalVar.motionConfig.boxReadInfo.have_OPcount;
                ckb_IsCheckInclude.Checked = GlobalVar.motionConfig.boxReadInfo.IsCheckInclude;
                #endregion
            }
            catch (Exception ex)
            {
                displalyInfo.displayInfoMain?.Invoke($"ControlToParameter error : {ex}");
            }
        }
        private TemplateParam Get_TemplateCreate()
        {
            var Template_Create = new TemplateParam();
            Template_Create.eMetric = (e_Metric)Enum.Parse(typeof(e_Metric), cbx_tc_eMetric.Text.Trim());
            Template_Create.eOptmization = (e_Optmization)Enum.Parse(typeof(e_Optmization), cbx_tc_eOptminzation.Text.Trim());
            Template_Create.nContras = (int)nud_tc_Contra.Value;
            Template_Create.nLevels = (int)nud_tc_nLevel.Value;
            Template_Create.nMinContrast = (int)nud_tc_MinContra.Value;
            return Template_Create;
        }
        private void set_TemplateCreate(TemplateParam Template_Create)
        {
            if (Template_Create == null)
                Template_Create = new TemplateParam();
            cbx_tc_eMetric.Text = Template_Create.eMetric.ToString();
            cbx_tc_eOptminzation.Text = Template_Create.eOptmization.ToString();
            nud_tc_Contra.Value = Template_Create.nContras;
            nud_tc_nLevel.Value = Template_Create.nLevels;
            nud_tc_MinContra.Value = Template_Create.nMinContrast;
        }
        private TemplateFindParam Get_TemplateFind()
        {
            var Template_Find = new TemplateFindParam();
            Template_Find.nLevels = (int)nud_tf_Level.Value;
            Template_Find.dGreediness = (double)nud_tf_Greediness.Value;
            Template_Find.dMaxOverlap = (double)nud_tf_MaxOverlap.Value;
            Template_Find.dMinScore = (double)nud_tf_MinScore.Value;
            Template_Find.nNumMatches = (int)nud_tf_NumMatch.Value;
            Template_Find.eSubPixel = (e_SubPixel)Enum.Parse(typeof(e_SubPixel), cbx_tf_eSubPixel.Text.Trim());
            return Template_Find;
        }
        private void set_TemplateFind(TemplateFindParam Template_Find)
        {
            if (Template_Find == null)
                Template_Find = new TemplateFindParam();
            nud_tf_Level.Value = Template_Find.nLevels;
            nud_tf_Greediness.Value = (decimal)Template_Find.dGreediness;
            nud_tf_MaxOverlap.Value = (decimal)Template_Find.dMaxOverlap;
            nud_tf_MinScore.Value = (decimal)Template_Find.dMinScore;
            nud_tf_NumMatch.Value = (decimal)Template_Find.nNumMatches;
            cbx_tf_eSubPixel.Text = Template_Find.eSubPixel.ToString();
        }
        private void btn_deleteAMname_Click(object sender, EventArgs e)
        {
            try
            {
                if (cbo_type_selectType.Items.Count < 1 || cbo_type_selectType.SelectedIndex < 0)
                {
                    displalyInfo.displayInfoMain("请先选择要删除的型号规格");
                    return;
                }
                DirectoryInfo dif = new DirectoryInfo(GlobalVar.type_path);
                foreach (var item in dif.GetFiles())
                {
                    if ((item.Name.Replace(".tfp", "")) == cbo_type_selectType.SelectedItem.ToString())
                    {
                        File.Delete(item.FullName);
                    }
                }
                cbo_type_selectType.Items.RemoveAt(cbo_type_selectType.SelectedIndex);
                cbo_type_selectType.SelectedIndex = 0;
                GlobalVar.closeSave.CurrentTypeName = cbo_type_selectType.SelectedItem.ToString();
                displalyInfo.displayInfoMain("所选配方已删除");
            }
            catch (System.Exception ex)
            {
                displalyInfo.displayInfoMain($"删除型号数据出错:{ex.Message}");
            }
        }

        private void btn_saveType_Click(object sender, EventArgs e)
        {
            var curType = cbo_type_selectType.Text.Trim();
            var curVersion = cbo_selectVersion.Text.Trim();
            if ( !string.IsNullOrEmpty(curType)&& !string.IsNullOrEmpty(curVersion))
            {
                if (GlobalVar.userConfig.UploadInfo.IsUploadUrl)
                {
                    if (!string.IsNullOrEmpty(GlobalVar.userConfig.UploadInfo.url_GetModelInfo))
                    {
                        var res = UploadInterface.upload_GetModelInfo();
                        if (res != null && res.Code == "00" && res.ModelInfoList != null && res.ModelInfoList.Count > 0)
                        {
                            var mlst = res.ModelInfoList.Where(a => a.Model== curType).ToList();
                        
                            if (mlst != null&& mlst.Count>0)
                            {
                                var curVer = curVersion;
                                var curConfig = "";
                                if(curVersion.IndexOf(" ")>0)
                                {
                                    curVer = curVersion.Split(' ')[0];
                                    curConfig = curVersion.Split(' ')[1];
                                }
                                var vlst=mlst[0].ModelVerInfoList.Where(a => a.Ver == curVer&&a.Config== curConfig).ToList();
                                if (vlst != null && vlst.Count > 0)
                                {
                                    bool isAdd = false;
                                    if (!cbo_type_selectType.Items.Contains(curType))
                                    {
                                        isAdd = true;
                                        cbo_type_selectType.Items.Add(curType);
                                        cbo_selectVersion.Items.Clear();
                                    }
                                    if (!cbo_selectVersion.Items.Contains(curVersion))
                                    {
                                        isAdd = true;
                                        cbo_selectVersion.Items.Add(curVersion);
                                    }
                                    if(isAdd)
                                        GlobalVar.tv_list.Add(new typeVersion() { type = curType, Version = curVersion });
                                    saveType();
                                    displalyInfo.displayInfoMain("型号保存成功");
                                    
                                }
                            }
                        }
                    }
                }
                else
                {
                    bool isAdd = false;
                    if (!cbo_type_selectType.Items.Contains(curType))
                    {
                        cbo_type_selectType.Items.Add(curType);
                        cbo_selectVersion.Items.Clear();
                        isAdd = true;
                    }
                    if (!cbo_selectVersion.Items.Contains(curVersion))
                    {
                        cbo_selectVersion.Items.Add(curVersion);
                        isAdd = true;
                      
                    }
                    if(isAdd)
                        GlobalVar.tv_list.Add(new typeVersion() { type = curType, Version = curVersion });
                    if (!string.IsNullOrEmpty(curType))
                    {
                        saveType();
                    }
                    displalyInfo.displayInfoMain("型号保存成功");
                }
            }
        
        }
        private void set_VisionCenter(VisionPos put)
        {
            if (put == null)
                put = new VisionPos();
            nud_VisionCenterX.Value = (decimal)put.X;
            nud_VisionCenterY.Value = (decimal)put.Y;

        }
        private VisionPos Get_VisionCenter()
        {

            var put = new VisionPos();
            put.X = (double)nud_VisionCenterX.Value;
            put.Y = (double)nud_VisionCenterY.Value;

            return put;
        }
        private void typeAddList()
        {
            try
            {

                if (!Directory.Exists(GlobalVar.type_path))
                {
                    return;
                }
                DirectoryInfo dif = new DirectoryInfo(GlobalVar.type_path);
                var arr_fileName = dif.GetFiles();
                foreach (var item in arr_fileName)
                {
                    if (item.Extension == ".tfp")
                    {
                        var str_type = item.Name.Replace(".tfp", "");
                        var arr_type=str_type.Split('~');
                        cbo_type_selectType.Items.Add(arr_type[0]);
                        var curType = new typeVersion() { type = arr_type[0] };
                       if (arr_type.Length>1)
                        {
                            curType.Version = arr_type[1];
                        }
                        GlobalVar.tv_list.Add(curType);
                    }
                }
                if (cbo_type_selectType.Items.Count > 0)
                {

                    if (!string.IsNullOrEmpty(GlobalVar.closeSave.CurrentTypeName))
                    {
                        var arr_type = GlobalVar.closeSave.CurrentTypeName.Split(GlobalVar.char_VersionSpilt);
                        cbo_type_selectType.SelectedItem = arr_type[0];
                        var lst_cur = GlobalVar.tv_list.Where(a => a.type == arr_type[0]&&!string.IsNullOrEmpty(a.Version)).Select(a => a.Version).ToList();
                        if (lst_cur.Count > 0)
                        {
                            cbo_selectVersion.Items.AddRange(lst_cur.ToArray());
                        }
                        if (arr_type.Length > 1)
                        {
                            if (lst_cur.Contains(arr_type[1]))
                            {
                                cbo_selectVersion.SelectedItem = arr_type[1];
                            }
                            else
                            {
                                if (lst_cur.Count > 0)
                                {
                                    cbo_selectVersion.SelectedIndex = 0;
                                }
                            }
                        }
                        else
                        {
                            if (lst_cur.Count > 0)
                            {
                                cbo_selectVersion.SelectedIndex = 0;
                            }
                        }
                    }
                    else
                    {
                        cbo_type_selectType.SelectedIndex = 0;
                        GlobalVar.closeSave.CurrentTypeName = cbo_type_selectType.SelectedItem.ToString();
                    }
                }

                if (cbo_type_selectType.SelectedItem != null)
                {
                    GlobalVar.motionConfig =(MotionConfig) SerializeHelper.DeserializeFile<MotionConfig>(GlobalVar.type_path + "\\" + GlobalVar.closeSave.CurrentTypeName + ".tfp", "型号参数");//运动参数反序列化
                    if (GlobalVar.motionConfig == null)
                    {
                        GlobalVar.motionConfig = new MotionConfig();
                    }
                }
            }
            catch (System.Exception ex)
            {
                displalyInfo.displayInfoMain($"添加型号到列表出错:{ex.Message}");
            }
        }

        private void saveType()
        {
            try
            {
                bool saveOld = false;
                var curType = cbo_type_selectType.Text.Trim();
                var curVision = cbo_selectVersion.Text.Trim();
                if (!cbo_type_selectType.Items.Contains(curType))
                {
                    cbo_type_selectType.Items.Add(curType);
                }
                if (!cbo_selectVersion.Items.Contains(curVision))
                {
                    cbo_selectVersion.Items.Add(curVision);
                }
                if (GlobalVar.motionConfig == null)
                {
                    GlobalVar.motionConfig = new MotionConfig();
                }
                ControlToParameter();
                if (!Directory.Exists(GlobalVar.type_path))
                {
                    Directory.CreateDirectory(GlobalVar.type_path);
                }
                if (!Directory.Exists(GlobalVar.type_copy_path))
                {
                    Directory.CreateDirectory(GlobalVar.type_copy_path);
                }
                GlobalVar.closeSave.CurrentTypeName = curType + GlobalVar.char_VersionSpilt + curVision;
                SerializeHelper.serializeFile(GlobalVar.type_path + "\\" + GlobalVar.closeSave.CurrentTypeName + ".tfp", GlobalVar.motionConfig, GlobalVar.closeSave.CurrentTypeName);
                SerializeHelper.serializeFile(GlobalVar.type_copy_path + "\\" + GlobalVar.closeSave.CurrentTypeName + ".tfp", GlobalVar.motionConfig, GlobalVar.closeSave.CurrentTypeName);
                
            }
            catch (System.Exception ex)
            {
                displalyInfo.displayInfoMain($"型号数据保存出错:{ex.Message}");
            }
        }

        /// <summary>
        /// 反序列化
        /// </summary>

        private bool isFirst = true;
        private void FormType_Load(object sender, EventArgs e)
        {
            if (isFirst)
            {
                rbn_plate1.Checked = true;
                rbn_plate2.Checked = false;
                rbn_v_plate1.Checked = true;
                rbn_v_plate2.Checked = false;
                rbn_pos.Checked = true;
                rbn_scan.Checked = false;
                this.rbn_plate1.CheckedChanged += new System.EventHandler(this.rbn_plate1_CheckedChanged);
                this.rbn_v_plate1.CheckedChanged += new System.EventHandler(this.rbn_v_plate1_CheckedChanged);
                this.rbn_pos.CheckedChanged += new System.EventHandler(this.rbn_v_plate1_CheckedChanged);
                this.rbn_check.CheckedChanged += new System.EventHandler(this.rbn_v_plate1_CheckedChanged);
                this.rbn_m_Plate1.CheckedChanged += new System.EventHandler(this.rbn_m_Plate1_CheckedChanged);
                rbn_m_Plate1.Checked = true;
                isFirst = false;
                SetCutLimit();
            }
            posCount = Enum.GetNames(typeof(e_Pos)).Length;
            posNGCount = Enum.GetNames(typeof(e_PosNG)).Length;
            typeAddList();
            ParameterToControl();
            displalyInfo.displayInfoMain("型号数据初始化");
            GlobalVar.ChangeModel+= ChangeModel;

            //    tabControl2.TabPages.RemoveAt(1);
            if (rbn_plate1.Checked)
            {
                SetLabelLimit((int)e_platePro.平台1);
            }
            else
            {
                SetLabelLimit((int)e_platePro.平台2);
            }
        }
        public int ChangeModel(string version)
        {
            try
            {
                if (!string.IsNullOrEmpty(version))
                {
                    GlobalVar.motionConfig = (MotionConfig)SerializeHelper.DeserializeFile<MotionConfig>(GlobalVar.type_path + "\\" + version + ".tfp", "型号参数");//运动参数反序列化
                    this.Invoke(new Action(ParameterToControl));
                    GlobalVar.closeSave.CurrentTypeName = version;
                    displalyInfo.displayInfoMain($"版本切换为{ GlobalVar.closeSave.CurrentTypeName }");
                    return 1;
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                displalyInfo.displayErrorMain($"版本切换为{ GlobalVar.closeSave.CurrentTypeName }失败");
                return -1;
            }
        }
        #region type setting

        private void FormType_FormClosing(object sender, FormClosingEventArgs e)
        {
            //SerializeFile_AutoLoad();
        }
        #endregion
        private void cbo_type_selectType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbo_type_selectType.SelectedIndex > -1)
            {
                var strSelect = cbo_type_selectType.Text;
                if (GlobalVar.closeSave.CurrentTypeName.StartsWith(strSelect + GlobalVar.char_VersionSpilt))
                    return;
                var tList= GlobalVar.tv_list.Where(a => a.type == strSelect).ToList();
                if (tList.Count > 0)
                {
                    cbo_selectVersion.Items.Clear();
                    cbo_selectVersion.Items.AddRange(tList.Select(a=>a.Version).ToArray());
                    cbo_selectVersion.SelectedIndex = 0;
                    var strVersion = cbo_selectVersion.Text;
                    var curtypeVerison = strSelect + "~" + strVersion;
                    GlobalVar.motionConfig = (MotionConfig)SerializeHelper.DeserializeFile<MotionConfig>(GlobalVar.type_path + "\\" + curtypeVerison + ".tfp", "型号参数");//运动参数反序列化
                    ParameterToControl();
                    GlobalVar.closeSave.CurrentTypeName = curtypeVerison;
                }
            }
        }



        private void rbn_plate1_CheckedChanged(object sender, EventArgs e)
        {
            if (rbn_plate1.Checked)
            {
                SetLabelLimit((int)e_platePro.平台1);
                SetPosList(GlobalVar.motionConfig.Plate1_pos);
               
            }
            else
            {
                SetLabelLimit((int)e_platePro.平台2);
                SetPosList(GlobalVar.motionConfig.Plate2_pos);
             
            }
        }

      

      

        private void btn_Move_GetLabelX_Click(object sender, EventArgs e)
        {
            if (rbn_plate1.Checked)
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标X1下].PointMove((double)nud_GetLabelX.Value, true);
            else
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标X2上].PointMove((double)nud_GetLabelX.Value, true);
        }

        private void btn_Move_GetLabelY_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标Y轴].PointMove((double)nud_GetLabelY.Value, true);
        }

        private void btn_Move_GetLabelR_Click(object sender, EventArgs e)
        {
            if (rbn_plate1.Checked)
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R1下].PointMove((double)nud_GetLabelR.Value, true);
            else
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R2上].PointMove((double)nud_GetLabelR.Value, true);
        }

        private void btn_Move_GetLabelZ_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标Z轴].PointMove((double)nud_GetLabelZ.Value, true);
        }

        private void btn_Move_scanX_Click(object sender, EventArgs e)
        {
            if (rbn_plate1.Checked)
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标X1下].PointMove((double)nud_pos_scanX.Value, true);
            else
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标X2上].PointMove((double)nud_pos_scanX.Value, true);

        }

        private void btn_Move_scanY_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标Y轴].PointMove((double)nud_pos_scanY.Value, true);
        }

        private void btn_Move_scanR_Click(object sender, EventArgs e)
        {
            if (rbn_plate1.Checked)
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R1下].PointMove((double)nud_pos_scanR.Value, true);
            else
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R2上].PointMove((double)nud_pos_scanR.Value, true);
        }

        private void btn_Move_scanZ_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标Z轴].PointMove((double)nud_pos_scanZ.Value, true);
        }

        private void btn_Move_NGX_Click(object sender, EventArgs e)
        {
            if (rbn_plate1.Checked)
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标X1下].PointMove((double)nud_pos_NGX.Value, true);
            else
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标X2上].PointMove((double)nud_pos_NGX.Value, true);
        }

        private void btn_Move_NGY_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标Y轴].PointMove((double)nud_pos_NGY.Value, true);
        }

        private void btn_Move_NGR_Click(object sender, EventArgs e)
        {
            if (rbn_plate1.Checked)
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R1下].PointMove((double)nud_pos_NGR.Value, true);
            else
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R2上].PointMove((double)nud_pos_NGR.Value, true);
        }

        private void btn_Move_NGZ_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标Z轴].PointMove((double)nud_pos_NGZ.Value, true);
        }

        private void btn_move_OKX_Click(object sender, EventArgs e)
        {
            if (rbn_plate1.Checked)
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标X1下].PointMove((double)nud_pos_OKX.Value, true);
            else
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标X2上].PointMove((double)nud_pos_OKX.Value, true);
        }

        private void btn_move_OKY_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标Y轴].PointMove((double)nud_pos_OKY.Value, true);
        }

        private void btn_move_OKR_Click(object sender, EventArgs e)
        {
            if (rbn_plate1.Checked)
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R1下].PointMove((double)nud_pos_OKR.Value, true);
            else
                GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标R2上].PointMove((double)nud_pos_OKR.Value, true);
        }

        private void btn_move_OKZ_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标Z轴].PointMove((double)nud_pos_OKZ.Value, true);
        }

        private void btn_Move_GetNGY_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标Y轴].PointMove((double)nud_NGGetY.Value, true);
        }

        private void btn_Move_GetNGZHeight_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.剔除Z轴].PointMove((double)nud_NGGetZup.Value, true);
        }

        private void btn_Move_GetNGZlow_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.剔除Z轴].PointMove((double)nud_NGGetZdown.Value, true);
        }

        private void btn_Move_PutNGYLow_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标Y轴].PointMove((double)nud_NGPutY.Value, true);
        }

        private void btn_Move_PutNGZHeight_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.剔除Z轴].PointMove((double)nud_NGPutZup.Value, true);
        }

        private void btn_Move_PutNGZLow_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.剔除Z轴].PointMove((double)nud_NGPutZdown.Value, true);
        }

      

        private void btn_Move_WaitLaserZ_MouseUp(object sender, MouseEventArgs e)
        {
            string str = ((Button)sender).Parent.Text + ((Button)sender).Text;
            displalyInfo.displayInfoMain(str);
        }

        private void btn_Draw_Click(object sender, EventArgs e)
        {
            var curSelect = GetSelect_T();
            if(curSelect==e_CameraPlate.定位相机平台1|| curSelect == e_CameraPlate.定位相机平台2)
            GlobalVar.Image_drawRoiRegion1();
            else if(curSelect == e_CameraPlate.图案相机平台1 || curSelect == e_CameraPlate.图案相机平台2)
            GlobalVar.Image_drawRoiRegion2();
            else if (curSelect == e_CameraPlate.检测相机平台1 || curSelect == e_CameraPlate.检测相机平台2)
                GlobalVar.Image_drawRoiRegion3();
        }

        private void btn_ReadROI_Click(object sender, EventArgs e)
        {
            var curSelect = GetSelect_T();
            visionProc.ReadRegion();
            if (curSelect == e_CameraPlate.定位相机平台1 || curSelect == e_CameraPlate.定位相机平台2)
            {
                visionProc.ROI1 = visionProc.ROI_List[(int)curSelect];
                GlobalVar.imageDisplay1_obj(visionProc.ROI1.ROI_Region, "green");
              
                GlobalVar.imageDisplay1_obj(visionProc.ROI1.ROI_Template, "blue");

                GlobalVar.imageDisplay1_obj(visionProc.ROI1.ROI_Postion, "orange");
                GlobalVar.imageDisplay1_obj(visionProc.ROI1.ROI_Postion2, "orange");
                GlobalVar.imageDisplay1_obj(visionProc.ROI1.ROI_PosCircle, "red");
            }
            if (curSelect == e_CameraPlate.图案相机平台1 || curSelect == e_CameraPlate.图案相机平台2)
            {
                visionProc.ROI2 = visionProc.ROI_List[(int)curSelect];
                GlobalVar.imageDisplay2_obj(visionProc.ROI2.ROI_Region, "green");
         
                GlobalVar.imageDisplay2_obj(visionProc.ROI2.ROI_Template, "blue");
            }
            if (curSelect == e_CameraPlate.检测相机平台1 || curSelect == e_CameraPlate.检测相机平台2)
            {
                visionProc.ROI3 = visionProc.ROI_List[(int)curSelect];
                GlobalVar.imageDisplay3_obj(visionProc.ROI3.ROI_Region, "green");
               
            }
        }

        private void btn_SaveROI_Click(object sender, EventArgs e)
        {
            var nRegion = cbx_Region.Items.IndexOf(cbx_Region.Text);
            if (nRegion > -1)
            {
                var arr = Enum.GetNames(typeof(e_Region));
                if (arr.Contains(cbx_Region.Text))
                {
                    var curSelect = GetSelect_T();
                    //if(rbn_v_plate1.Checked)
                      visionProc.SaveRegion((e_Region)Enum.Parse(typeof(e_Region), cbx_Region.Text), curSelect);
                    if(nRegion>=(int)e_Region.直线区域1)
                    {
                        if(curSelect==e_CameraPlate.定位相机平台1)
                        {
                            var curROI = visionProc.ROI_List[(int)curSelect];
                            if (curROI.ROI_Template!= null&& curROI.ROI_Template.CountObj()>0)
                            {
                                if (nRegion == (int)e_Region.直线区域1)
                                {
                                    HOperatorSet.SmallestRectangle1(curROI.ROI_Template, out HTuple row1, out HTuple col1, out HTuple row2, out HTuple col2);
                                    HOperatorSet.SmallestRectangle2(curROI.ROI_Postion, out HTuple rowPos, out HTuple colPos,out HTuple phi, out HTuple rowL2, out HTuple colL2);
                                    if (row1.Type != HTupleType.EMPTY && rowPos.Type != HTupleType.EMPTY)
                                    {
                                        var baseX = (col1.D + col2.D) / 2;
                                        var baseY = (row1.D + row2.D) / 2;
                                        //var nextX = (colL1.D + colL2.D) / 2;
                                        //var nextY = (rowL1.D + rowL2.D) / 2;
                                        var nextX = colPos.D;
                                        var nextY = rowPos.D;

                                        var basePoint = new PointF() { X = (float)baseX, Y = (float)baseY };
                                        var nextPoint = new PointF() { X = (float)nextX, Y = (float)nextY };
                                        var infolst=visionProc.CaculateRangle(basePoint, nextPoint);
                                        if(infolst.Count>1)
                                        {
                                            GlobalVar.motionConfig.PosLindFind1[nRegion - (int)e_Region.直线区域1].R = infolst[0];
                                            GlobalVar.motionConfig.PosLindFind1[nRegion - (int)e_Region.直线区域1].Angle = infolst[1];
                                        }
                                        GlobalVar.motionConfig.PosLindFind1[nRegion - (int)e_Region.直线区域1].VarX = (col1.D + col2.D) / 2;
                                        GlobalVar.motionConfig.PosLindFind1[nRegion - (int)e_Region.直线区域1].VarY = (row1.D + row2.D) / 2;
                                    }
                                }
                                if (nRegion == (int)e_Region.直线区域2)
                                {
                                    HOperatorSet.SmallestRectangle1(curROI.ROI_Template, out HTuple row1, out HTuple col1, out HTuple row2, out HTuple col2);
                                    HOperatorSet.SmallestRectangle2(curROI.ROI_Postion2, out HTuple rowPos, out HTuple colPos, out HTuple phi, out HTuple rowL2, out HTuple colL2);
                                    if (row1.Type != HTupleType.EMPTY && rowPos.Type != HTupleType.EMPTY)
                                    {
                                        var baseX = (col1.D + col2.D) / 2;
                                        var baseY = (row1.D + row2.D) / 2;
                                        //var nextX = (colL1.D + colL2.D) / 2;
                                        //var nextY = (rowL1.D + rowL2.D) / 2;
                                        var nextX = colPos.D;
                                        var nextY = rowPos.D;

                                        var basePoint = new PointF() { X = (float)baseX, Y = (float)baseY };
                                        var nextPoint = new PointF() { X = (float)nextX, Y = (float)nextY };
                                        var infolst = visionProc.CaculateRangle(basePoint, nextPoint);
                                        if (infolst.Count > 1)
                                        {
                                            GlobalVar.motionConfig.PosLindFind1[nRegion - (int)e_Region.直线区域1].R = infolst[0];
                                            GlobalVar.motionConfig.PosLindFind1[nRegion - (int)e_Region.直线区域1].Angle = infolst[1];
                                        }
                                        GlobalVar.motionConfig.PosLindFind1[nRegion - (int)e_Region.直线区域1].VarX = (col1.D + col2.D) / 2;
                                        GlobalVar.motionConfig.PosLindFind1[nRegion - (int)e_Region.直线区域1].VarY = (row1.D + row2.D) / 2;
                                    }
                                }
                                if (nRegion == (int)e_Region.抓圆区域)
                                {
                                    HOperatorSet.SmallestRectangle1(curROI.ROI_Template, out HTuple row1, out HTuple col1, out HTuple row2, out HTuple col2);
                                    HOperatorSet.SmallestRectangle2(curROI.ROI_PosCircle, out HTuple rowPos, out HTuple colPos, out HTuple phi, out HTuple rowL2, out HTuple colL2);
                                    if (row1.Type != HTupleType.EMPTY && rowPos.Type != HTupleType.EMPTY)
                                    {
                                        var baseX = (col1.D + col2.D) / 2;
                                        var baseY = (row1.D + row2.D) / 2;
                                        //var nextX = (colL1.D + colL2.D) / 2;
                                        //var nextY = (rowL1.D + rowL2.D) / 2;
                                        var nextX = colPos.D;
                                        var nextY = rowPos.D;

                                        var basePoint = new PointF() { X = (float)baseX, Y = (float)baseY };
                                        var nextPoint = new PointF() { X = (float)nextX, Y = (float)nextY };
                                        var infolst = visionProc.CaculateRangle(basePoint, nextPoint);
                                        if (infolst.Count > 1)
                                        {
                                            GlobalVar.motionConfig.PosCircleFindCheck1[nRegion - (int)e_Region.抓圆区域].R = infolst[0];
                                            GlobalVar.motionConfig.PosCircleFindCheck1[nRegion - (int)e_Region.抓圆区域].Angle = infolst[1];
                                        }
                                        GlobalVar.motionConfig.PosCircleFindCheck1[nRegion - (int)e_Region.抓圆区域].VarX = (col1.D + col2.D) / 2;
                                        GlobalVar.motionConfig.PosCircleFindCheck1[nRegion - (int)e_Region.抓圆区域].VarY = (row1.D + row2.D) / 2;
                                    }
                                }
                            }
                        }
                        else if (curSelect == e_CameraPlate.定位相机平台2)
                        {
                            var curROI = visionProc.ROI_List[(int)curSelect];
                            if (curROI.ROI_Template != null && curROI.ROI_Template.CountObj() > 0)
                            {
                                if (nRegion == (int)e_Region.直线区域1)
                                {
                                    HOperatorSet.SmallestRectangle1(curROI.ROI_Template, out HTuple row1, out HTuple col1, out HTuple row2, out HTuple col2);
                                    HOperatorSet.SmallestRectangle2(curROI.ROI_Postion, out HTuple rowPos, out HTuple colPos, out HTuple phi, out HTuple rowL2, out HTuple colL2);
                                    if (row1.Type != HTupleType.EMPTY && rowPos.Type != HTupleType.EMPTY)
                                    {
                                        var baseX = (col1.D + col2.D) / 2;
                                        var baseY = (row1.D + row2.D) / 2;
                                        //var nextX = (colL1.D + colL2.D) / 2;
                                        //var nextY = (rowL1.D + rowL2.D) / 2;
                                        var nextX = colPos.D;
                                        var nextY = rowPos.D;

                                        var basePoint = new PointF() { X = (float)baseX, Y = (float)baseY };
                                        var nextPoint = new PointF() { X = (float)nextX, Y = (float)nextY };
                                        var infolst = visionProc.CaculateRangle(basePoint, nextPoint);
                                        if (infolst.Count > 1)
                                        {
                                            GlobalVar.motionConfig.PosLindFind2[nRegion - (int)e_Region.直线区域1].R = infolst[0];
                                            GlobalVar.motionConfig.PosLindFind2[nRegion - (int)e_Region.直线区域1].Angle = infolst[1];
                                        }
                                        GlobalVar.motionConfig.PosLindFind2[nRegion - (int)e_Region.直线区域1].VarX = (col1.D + col2.D) / 2;
                                        GlobalVar.motionConfig.PosLindFind2[nRegion - (int)e_Region.直线区域1].VarY = (row1.D + row2.D) / 2;
                                    }
                                }
                                if (nRegion == (int)e_Region.直线区域2)
                                {
                                    HOperatorSet.SmallestRectangle1(curROI.ROI_Template, out HTuple row1, out HTuple col1, out HTuple row2, out HTuple col2);
                                    
                                    HOperatorSet.SmallestRectangle2(curROI.ROI_Postion2, out HTuple rowPos, out HTuple colPos, out HTuple phi, out HTuple rowL2, out HTuple colL2);
                                    if (row1.Type != HTupleType.EMPTY && rowPos.Type != HTupleType.EMPTY)
                                    {
                                        var baseX = (col1.D + col2.D) / 2;
                                        var baseY = (row1.D + row2.D) / 2;
                                        //var nextX = (colL1.D + colL2.D) / 2;
                                        //var nextY = (rowL1.D + rowL2.D) / 2;
                                        var nextX = colPos.D;
                                        var nextY = rowPos.D;

                                        var basePoint = new PointF() { X = (float)baseX, Y = (float)baseY };
                                        var nextPoint = new PointF() { X = (float)nextX, Y = (float)nextY };
                                        var infolst = visionProc.CaculateRangle(basePoint, nextPoint);
                                        if (infolst.Count > 1)
                                        {
                                            GlobalVar.motionConfig.PosLindFind2[nRegion - (int)e_Region.直线区域1].R = infolst[0];
                                            GlobalVar.motionConfig.PosLindFind2[nRegion - (int)e_Region.直线区域1].Angle = infolst[1];
                                        }
                                        GlobalVar.motionConfig.PosLindFind2[nRegion - (int)e_Region.直线区域1].VarX = (col1.D + col2.D) / 2;
                                        GlobalVar.motionConfig.PosLindFind2[nRegion - (int)e_Region.直线区域1].VarY = (row1.D + row2.D) / 2;
                                    }
                                }
                                if (nRegion == (int)e_Region.抓圆区域)
                                {
                                    HOperatorSet.SmallestRectangle1(curROI.ROI_Template, out HTuple row1, out HTuple col1, out HTuple row2, out HTuple col2);
                                    HOperatorSet.SmallestRectangle2(curROI.ROI_PosCircle, out HTuple rowPos, out HTuple colPos, out HTuple phi, out HTuple rowL2, out HTuple colL2);
                                    if (row1.Type != HTupleType.EMPTY && rowPos.Type != HTupleType.EMPTY)
                                    {
                                        var baseX = (col1.D + col2.D) / 2;
                                        var baseY = (row1.D + row2.D) / 2;
                                        //var nextX = (colL1.D + colL2.D) / 2;
                                        //var nextY = (rowL1.D + rowL2.D) / 2;
                                        var nextX = colPos.D;
                                        var nextY = rowPos.D;

                                        var basePoint = new PointF() { X = (float)baseX, Y = (float)baseY };
                                        var nextPoint = new PointF() { X = (float)nextX, Y = (float)nextY };
                                        var infolst = visionProc.CaculateRangle(basePoint, nextPoint);
                                        if (infolst.Count > 1)
                                        {
                                            GlobalVar.motionConfig.PosCircleFindCheck2[nRegion - (int)e_Region.抓圆区域].R = infolst[0];
                                            GlobalVar.motionConfig.PosCircleFindCheck2[nRegion - (int)e_Region.抓圆区域].Angle = infolst[1];
                                        }
                                        GlobalVar.motionConfig.PosCircleFindCheck2[nRegion - (int)e_Region.抓圆区域].VarX = (col1.D + col2.D) / 2;
                                        GlobalVar.motionConfig.PosCircleFindCheck2[nRegion - (int)e_Region.抓圆区域].VarY = (row1.D + row2.D) / 2;
                                    }
                                }
                            }
                        }
                    }
                    displalyInfo.displayInfoMain($"{curSelect.ToString()}区域：{cbx_Region.Text} 保存成功");
                    return;
                }

            }
            displalyInfo.displayErrorMain($"区域：{cbx_Region.Text}保存失败");
        }

        private void btn_ReadTemplate_Click(object sender, EventArgs e)
        {
            visionProc.ReadTemplateRegion();
        }

        private void btn_SaveTemplate_Click(object sender, EventArgs e)
        {
              var curSelect = GetSelect_T();
                visionProc.SaveTemplateRegion(visionProc.TemplateIds[(int)curSelect], curSelect);
        }

        private void btn_CreateTemplate_Click(object sender, EventArgs e)
        {
            var curSelect = GetSelect_T();
            var param = Get_TemplateCreate();
           if(curSelect==e_CameraPlate.定位相机平台1||curSelect==e_CameraPlate.定位相机平台2)
               visionProc.TemplateIds[(int)curSelect] = visionProc.CreateTemplate(visionProc.srcObject_pos,visionProc.ROI1.ROI_Template, param,e_Camera.定位相机);
            else if (curSelect == e_CameraPlate.图案相机平台1 || curSelect == e_CameraPlate.图案相机平台2)
                visionProc.TemplateIds[(int)curSelect] = visionProc.CreateTemplate(visionProc.srcObject_scan, visionProc.ROI2.ROI_Template, param,e_Camera.图案相机);
        }

        private void btn_CheckTemplate_Click(object sender, EventArgs e)
        {
           
        }

        private void btn_reduce_Click(object sender, EventArgs e)
        {
            visionProc.isReduce = true;
            visionProc.reduce_Radius = (int)nud_radiusReduce.Value;
        }

        private void btn_reduceOK_Click(object sender, EventArgs e)
        {
            visionProc.isReduce = false;
        }
        private void btn_ReadImage_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "All File(*.*)|*.*";
                ofd.Title = "选择打印模板文件";
                ofd.InitialDirectory = Application.StartupPath;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    HOperatorSet.ReadImage(out HObject temp, ofd.FileName);
                    //HOperatorSet.GenRectangle1(out HObject rectangle, 0, 350, 3650, 5250);
                    //HOperatorSet.ReduceDomain(visionProc.srcObject_pos, rectangle, out HObject TEMP);
                    //HOperatorSet.CropDomain(TEMP, out visionProc.srcObject_pos);

                    //HOperatorSet.ChangeRadialDistortionImage(temp, temp,
                    //                   out visionProc.srcObject_pos,visionProc. hv_CameraParameters, visionProc. hv_CameraZero);

                     //HOperatorSet.MapImage(temp, visionProc.mapObject, out visionProc.srcObject_pos);
                    //HOperatorSet.RotateImage(temp, out visionProc.srcObject_pos, 180, "constant");
                    visionProc.srcObject_pos = temp;
                    GlobalVar.imageDisplay1(visionProc.srcObject_pos);
                }
            }
            catch (Exception ex)
            {
                displalyInfo.displayInfoMain($"{((Button)sender).Text}-[error] {ex.Message}");
            }
        }

        private void groupBox12_Enter(object sender, EventArgs e)
        {

        }

        private void btn_ImageProccess_Click(object sender, EventArgs e)
        {

        }

        private void label18_Click(object sender, EventArgs e)
        {

        }

        private void nud_pos_scanR_ValueChanged(object sender, EventArgs e)
        {

        }
        private void btn_cut_WaitX_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜X].PointMove((double)nud_cut_WaitX.Value, true);
        }

        private void btn_cut_WaitY_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜Y].PointMove((double)nud_cut_WaitY.Value, true);
        }

        private void btn_cut_WaitZ_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜Z].PointMove((double)nud_cut_WaitZ.Value, true);
        }

        private void btn_cut_WaitR1_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜R1].PointMove((double)nud_cut_WaitR1.Value, true);
        }

        private void btn_cut_WaitR2_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜R2].PointMove((double)nud_cut_WaitR2.Value, true);
        }

        private void btn_cut_ProX_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜X].PointMove((double)nud_cut_ProX.Value, true);
        }

        private void btn_cut_ProY_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜Y].PointMove((double)nud_cut_ProY.Value, true);
        }

        private void btn_cut_ProZ_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜Z].PointMove((double)nud_cut_ProZ.Value, true);
        }

        private void btn_cut_ProR1_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜R1].PointMove((double)nud_cut_ProR1.Value, true);
        }

        private void btn_cut_ProR2_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜R2].PointMove((double)nud_cut_ProR2.Value, true);
        }

        private void btn_cut_ClampX_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜X].PointMove((double)nud_cut_ClampX.Value, true);
        }

        private void btn_cut_ClampY_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜Y].PointMove((double)nud_cut_ClampY.Value, true);
        }

        private void btn_cut_ClampZ_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜Z].PointMove((double)nud_cut_ClampZ.Value, true);
        }

        private void btn_cut_ClampR1_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜R1].PointMove((double)nud_cut_ClampR1.Value, true);
        }

        private void btn_cut_ClampR2_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜R2].PointMove((double)nud_cut_ClampR2.Value, true);
        }

        private void btn_cut_EndX_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜X].PointMove((double)nud_cut_EndX.Value, true);
        }

        private void btn_cut_EndY_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜Y].PointMove((double)nud_cut_EndY.Value, true);
        }

        private void btn_cut_EndZ_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜Z].PointMove((double)nud_cut_EndZ.Value, true);
        }

        private void btn_cut_EndR1_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜R1].PointMove((double)nud_cut_EndR1.Value, true);
        }

        private void btn_cut_EndR2_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜R2].PointMove((double)nud_cut_EndR2.Value, true);
        }

        private void btn_cut_NGX_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜X].PointMove((double)nud_cut_NGX.Value, true);
        }

        private void btn_cut_NGY_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜Y].PointMove((double)nud_cut_NGY.Value, true);
        }

        private void btn_cut_NGZ_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜Z].PointMove((double)nud_cut_NGZ.Value, true);
        }

        private void btn_cut_NGR1_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜R1].PointMove((double)nud_cut_NGR1.Value, true);
        }

        private void btn_cut_NG2_Click(object sender, EventArgs e)
        {

            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜R2].PointMove((double)nud_cut_NGR2.Value, true);
        }

        private void btn_cut_mediumX_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜X].PointMove((double)nud_cut_mediumX.Value, true);
        }

        private void btn_cut_mediumY_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜Y].PointMove((double)nud_cut_mediumY.Value, true);
        }

        private void btn_cut_mediumZ_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜Z].PointMove((double)nud_cut_mediumZ.Value, true);
        }

        private void btn_cut_mediumR1_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜R1].PointMove((double)nud_cut_mediumR1.Value, true);
        }

        private void btn_cut_mediumR2_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜R2].PointMove((double)nud_cut_mediumR2.Value, true);
        }

        private void btn_upProZ_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.一段压产品Z].PointMove((double)nud_upProZ.Value, true);
        }

        private void btn_downProZ_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.一段压产品Z].PointMove((double)nud_downProZ.Value, true);
        }

        private void btn_m_readTemplateImage_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "All File(*.*)|*.*";
                ofd.Title = "选择检测相机图片";
                ofd.InitialDirectory = Application.StartupPath;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    HOperatorSet.ReadImage(out visionProc.srcObject_ckeck, ofd.FileName);
                    //         HOperatorSet.RotateImage(visionProc.srcObject_ckeck, out visionProc.srcObject_ckeck,3, "constant");

                    //HOperatorSet.HomMat2dIdentity(out HTuple hom2d);
                    //HOperatorSet.HomMat2dRotate(hom2d, 0, 10, 10, out HTuple hom2dRotate);
                    //HOperatorSet.AffineTransImage(visionProc.srcObject_ckeck, out visionProc.srcObject_ckeck, hom2dRotate, "constant","true");
                    GlobalVar.imageDisplay3(visionProc.srcObject_ckeck);
                }
            }
            catch (Exception ex)
            {
                displalyInfo.displayInfoMain($"{((Button)sender).Text}-[error] {ex.Message}");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var nRegion = cbx_m_RegionSelect.Items.IndexOf(cbx_m_RegionSelect.Text);
            if (nRegion > -1)
            {
                var arr = Enum.GetNames(typeof(e_RegionCheck));
                var curSelect = GetSelect_M();
                visionProc.SaveRegion_M((e_RegionCheck)Enum.Parse(typeof(e_RegionCheck), arr[nRegion]), curSelect);
                displalyInfo.displayInfoMain($"{curSelect.ToString()}区域：{arr[nRegion]} 保存成功");
            }
        }

        
        private void rbn_v_plate1_CheckedChanged(object sender, EventArgs e)
        {
            var curSelect = GetSelect_T();
            displalyInfo.displayInfoMain($"{curSelect.ToString()}");
            set_TemplateCreate(GlobalVar.motionConfig.TC_list[(int)curSelect]);
            set_TemplateFind(GlobalVar.motionConfig.TF_list[(int)curSelect]);
            if (curSelect == e_CameraPlate.定位相机平台1)
            {
                set_VisionCenter(GlobalVar.motionConfig.visionCenter1);
                set_VisionPut(GlobalVar.motionConfig.visionPutOK1);
                set_VisionBase(GlobalVar.motionConfig.visionBase1);
            }
            else if (curSelect == e_CameraPlate.定位相机平台2)
            {
                set_VisionCenter(GlobalVar.motionConfig.visionCenter2);
                set_VisionPut(GlobalVar.motionConfig.visionPutOK2);
                set_VisionBase(GlobalVar.motionConfig.visionBase2);
            }
            else if (curSelect == e_CameraPlate.图案相机平台1)
            {
                SetDrawInfo(GlobalVar.motionConfig.draw1);
            }
            else if (curSelect == e_CameraPlate.图案相机平台2)
            {
                SetDrawInfo(GlobalVar.motionConfig.draw2);
            }
        }

        private void btn_image_Click(object sender, EventArgs e)
        {
            var curSelect = GetSelect_T();
            visionProc.TemplateParam_Find = Get_TemplateFind();
            visionProc.ReadRegion();
            switch (curSelect)
            {
                case e_CameraPlate.定位相机平台1:
                case e_CameraPlate.定位相机平台2:
                    visionProc.curCameraPlate1 = curSelect;
                    GlobalVar.cameraList[(int)e_Camera.定位相机].softTrigger();
                    break;
                case e_CameraPlate.图案相机平台1:
                case e_CameraPlate.图案相机平台2:
                    visionProc.curCameraPlate2 = curSelect;
                    GlobalVar.cameraList[(int)e_Camera.图案相机].softTrigger();
                    break;
                case e_CameraPlate.检测相机平台1:
                case e_CameraPlate.检测相机平台2:
                    visionProc.curCameraPlate3 = curSelect;
                    GlobalVar.cameraList[(int)e_Camera.检测相机].softTrigger();
                    break;
            }
         
        }

        private void btn_ImageProccess_Click_1(object sender, EventArgs e)
        {

        }

        private void btn_m_Draw_Click(object sender, EventArgs e)
        {
            GlobalVar.Image_drawRoiRegion3();
        }

        private void btn_m_CheckTemplate_Click(object sender, EventArgs e)
        {

        }

        private void btn_CheckTemplate_Click_1(object sender, EventArgs e)
        {
            var select = GetSelect_T();
            visionProc.TemplateParam_Find = Get_TemplateFind();
            if (select == e_CameraPlate.定位相机平台1 || select == e_CameraPlate.定位相机平台2)
            {
            
                var res = visionProc.findTemplate(visionProc.srcObject_pos, visionProc.TemplateIds[(int)select], visionProc.TemplateParam_Find, visionProc.ROI_List[(int)select].ROI_Region);
                if (res != null)
                    displalyInfo.displayInfoMain(JsonConvert.SerializeObject(res));
            }
            else if (select == e_CameraPlate.图案相机平台1 || select == e_CameraPlate.图案相机平台2)
            {
                HOperatorSet.ReduceDomain(visionProc.srcObject_scan, visionProc.ROI_List[(int)e_CameraPlate.图案相机平台1].ROI_Region, out HObject region_cut);
                HOperatorSet.Threshold(region_cut, out HObject AreaHobj, GlobalVar.motionConfig.draw1.d_grayMin, GlobalVar.motionConfig.draw1.d_grayMax);
                HOperatorSet.AreaCenter(AreaHobj, out HTuple hArea, out HTuple tempR, out HTuple tempC);
                if (hArea.Type != HTupleType.EMPTY)
                {
                    var res = visionProc.findTemplate(visionProc.srcObject_scan, visionProc.TemplateIds[(int)select], visionProc.TemplateParam_Find, visionProc.ROI_List[(int)select].ROI_Region);
                
                    if (res != null)
                        displalyInfo.displayInfoMain(JsonConvert.SerializeObject(res));
                }
            }
           
        }

        private void btn_m_ReadTemplate_Click(object sender, EventArgs e)
        {

        }

        private void btn_clearRoi_Click(object sender, EventArgs e)
        {
            var curSelect = GetSelect_T();
            if (curSelect == e_CameraPlate.定位相机平台1 || curSelect == e_CameraPlate.定位相机平台2)
                GlobalVar.Image_clearRoiRegion1();
            else if (curSelect == e_CameraPlate.图案相机平台1 || curSelect == e_CameraPlate.图案相机平台2)
                GlobalVar.Image_clearRoiRegion2();
        }



        private void btn_move_scanSafeX1_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标X1下].PointMove((double)nud_scanSafeX1.Value, true);
        }

        private void btn_move_scanSafeX2_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标X2上].PointMove((double)nud_scanSafeX2.Value, true);
        }

        private void btn_m_ReadROI_Click(object sender, EventArgs e)
        {
            var curSelect = GetSelect_M();
            var strArr = Enum.GetNames(typeof(e_RegionCheck));
            if (curSelect == e_CameraPlate.检测相机平台1)
            {
                visionProc.ROI_M_List1 = visionProc.ReadRegion_M(e_CameraPlate.检测相机平台1);
                if (visionProc.ROI_M_List1 != null && visionProc.ROI_M_List1.Count >= strArr.Length)
                {
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List1[(int)e_RegionCheck.切图区域], "green");
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List1[(int)e_RegionCheck.撕膜区域1],  "red");
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List1[(int)e_RegionCheck.撕膜区域2], "red");
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List1[(int)e_RegionCheck.撕膜区域3], "red");
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List1[(int)e_RegionCheck.检测区域1], "blue");
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List1[(int)e_RegionCheck.检测区域2], "blue");
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List1[(int)e_RegionCheck.检测区域3], "blue");
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List1[(int)e_RegionCheck.检测区域4], "blue");
                }
            }
            else if (curSelect == e_CameraPlate.检测相机平台2)
            {
                visionProc.ROI_M_List2 = visionProc.ReadRegion_M(e_CameraPlate.检测相机平台2);
                if (visionProc.ROI_M_List2 != null && visionProc.ROI_M_List2.Count >= strArr.Length)
                {
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List2[(int)e_RegionCheck.切图区域], "green");
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List2[(int)e_RegionCheck.撕膜区域1], "red");
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List2[(int)e_RegionCheck.撕膜区域2], "red");
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List2[(int)e_RegionCheck.撕膜区域3], "red");
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List2[(int)e_RegionCheck.检测区域1], "blue");
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List2[(int)e_RegionCheck.检测区域2], "blue");
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List2[(int)e_RegionCheck.检测区域3], "blue");
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List2[(int)e_RegionCheck.检测区域4], "blue");
                }
            }
        }

        private void cbx_m_RegionSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rbn_m_Plate1.Checked)
            {
                if (cbx_m_RegionSelect.SelectedIndex > -1 && cbx_m_RegionSelect.SelectedIndex <= visionProc.ROI_M_List1.Count)
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List1[cbx_m_RegionSelect.SelectedIndex], "green");
                if (cbx_m_RegionSelect.SelectedIndex >= (int)e_RegionCheck.检测区域1) ;
                     Set_Measure(GlobalVar.motionConfig.vision_M1_list[cbx_m_RegionSelect.SelectedIndex]);
                if (cbx_m_RegionSelect.SelectedIndex >= (int)e_RegionCheck.找圆区域1 && cbx_m_RegionSelect.SelectedIndex <= (int)e_RegionCheck.找圆区域2)
                    Set_CircleParam(GlobalVar.motionConfig.CircleFindCheck1[cbx_m_RegionSelect.SelectedIndex - (int)e_RegionCheck.找圆区域1]);
            }
            else
            {
                if (cbx_m_RegionSelect.SelectedIndex > -1 && cbx_m_RegionSelect.SelectedIndex <= visionProc.ROI_M_List2.Count)
                    GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List2[cbx_m_RegionSelect.SelectedIndex], "green");
                if (cbx_m_RegionSelect.SelectedIndex >= (int)e_RegionCheck.检测区域1) ;
                Set_Measure(GlobalVar.motionConfig.vision_M2_list[cbx_m_RegionSelect.SelectedIndex]);
                if (cbx_m_RegionSelect.SelectedIndex >= (int)e_RegionCheck.找圆区域1 && cbx_m_RegionSelect.SelectedIndex <= (int)e_RegionCheck.找圆区域2)
                    Set_CircleParam(GlobalVar.motionConfig.CircleFindCheck2[cbx_m_RegionSelect.SelectedIndex - (int)e_RegionCheck.找圆区域1]);

            }
        }

        private void btn_clearRegion_Click(object sender, EventArgs e)
        {
            GlobalVar.Image_clearRoiRegion3();
        }

        private void btn_cameraTest_Click(object sender, EventArgs e)
        {
            var nRegionSelect = cbx_m_RegionSelect.Text;
            if (!string.IsNullOrEmpty(nRegionSelect) && cbx_m_RegionSelect.Items.IndexOf(nRegionSelect) > -1)
            {
                var IndexSelect = cbx_m_RegionSelect.Items.IndexOf(nRegionSelect);
                var arrStr = Enum.GetNames(typeof(e_RegionCheck));
                var e_Region = (e_RegionCheck)Enum.Parse(typeof(e_RegionCheck), arrStr[IndexSelect]);
                if (rbn_m_Plate1.Checked)
                {
                    if (IndexSelect > 1)
                    {
                        GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List1[IndexSelect], "green");
                        var res = visionProc.GetMeasures(e_CameraPlate.检测相机平台1, Get_Measure(), e_Region);
                        var n = res == null ? 0 : res.Count;
                        displalyInfo.displayInfoMain($"该区域有{n}匹配对" + (n > 0 ? string.Join(",",res) : 0.ToString()));
                    }else
                    {
                        HOperatorSet.ReduceDomain(visionProc.srcObject_ckeck, visionProc.ROI_M_List1[(int)e_RegionCheck.切图区域], out HObject cutObj);
                        HOperatorSet.Threshold(cutObj, out HObject threadObj, (int)nud_m_grayMin.Value, (int)nud_m_grayMax.Value);
                        GlobalVar.imageDisplay3_obj(threadObj, "red");
                        HOperatorSet.GetRegionPoints(threadObj, out HTuple rs, out HTuple cs);
                        bool IsHave = rs.Length > (int)nud_m_AreaMin.Value && rs.Length < (int)nud_m_AreaMax.Value;
                        displalyInfo.displayInfoMain($"有效面积为{rs.Length} *****为无文字板*****");
                    }
                }
                else
                {
                    if (IndexSelect > 1)
                    {
                        GlobalVar.imageDisplay3_obj(visionProc.ROI_M_List2[IndexSelect], "green");
                        var res = visionProc.GetMeasures(e_CameraPlate.检测相机平台2, Get_Measure(), e_Region);
                        var n = res == null ? 0 : res.Count;
                        displalyInfo.displayInfoMain($"该区域有{n}匹配对" + (n > 0 ? string.Join(",", res) : 0.ToString()));
                    }
                    else
                    {
                        HOperatorSet.ReduceDomain(visionProc.srcObject_ckeck, visionProc.ROI_M_List2[(int)e_RegionCheck.切图区域], out HObject cutObj);
                        HOperatorSet.Threshold(cutObj, out HObject threadObj, (int)nud_m_grayMin.Value, (int)nud_m_grayMax.Value);
                        GlobalVar.imageDisplay3_obj(threadObj, "red");
                        HOperatorSet.GetRegionPoints(threadObj, out HTuple rs, out HTuple cs);
                        bool IsHave = rs.Length > (int)nud_m_AreaMin.Value && rs.Length < (int)nud_m_AreaMax.Value;
                        displalyInfo.displayInfoMain($"有效面积为{rs.Length} *****为无文字板*****");
                    }
                }
            }
        }
        private void Set_Measure(MeasureParma param)
        {
            if (param == null)
                param = new MeasureParma();
            nud_m_Signal.Value =(decimal)param.dSigma;
            nud_m_Threshold.Value = (decimal)param.dThreshold;
            cbx_m_Interpolation.Text = param.Interpolation;
            cbx_m_Select.Text = param.Select;
            cbx_m_Transition.Text = param.Transition;
            nud_m_distanceMax.Value = (decimal)param.distanceMax;
            nud_m_distanceMin.Value = (decimal)param.distanceMin;
            nud_m_npar.Value = param.nPar;
        }
        private void Set_Cut(CutParma param)
        {
            if (param == null)
                param = new CutParma();
            nud_m_AreaMax.Value = param.AreaMax;
            nud_m_AreaMin.Value = param.AreaMin;
            nud_m_grayMax.Value = param.GaryMax;
            nud_m_grayMin.Value = param.GaryMin;
           
        }
        private CutParma Get_Cut()
        {
            var param = new CutParma();
            param.AreaMax = (int)nud_m_AreaMax.Value;
            param.AreaMin = (int)nud_m_AreaMin.Value;
            param.GaryMax = (int)nud_m_grayMax.Value;
            param.GaryMin = (int)nud_m_grayMin.Value;
            return param;
        }
        private MeasureParma Get_Measure()
        {
            var param = new MeasureParma();
            param.RegionIndex = cbx_m_RegionSelect.SelectedIndex;
            param.dSigma = (double)nud_m_Signal.Value;
            param.dThreshold = (double)nud_m_Threshold.Value;
            param.Interpolation = cbx_m_Interpolation.Text;
            param.Select = cbx_m_Select.Text;
            param.Transition = cbx_m_Transition.Text;
            param.nPar =(int) nud_m_npar.Value;
            param.distanceMax = (double)nud_m_distanceMax.Value;
            param.distanceMin = (double)nud_m_distanceMin.Value;
            return param;
        }

        private void rbn_m_Plate1_CheckedChanged(object sender, EventArgs e)
        {
            if (rbn_m_Plate1.Checked)
            {
                if (cbx_m_RegionSelect.SelectedIndex > -1)
                {
                    if(GlobalVar.motionConfig.vision_M1_list != null&& GlobalVar.motionConfig.vision_M1_list.Count>(cbx_m_RegionSelect.SelectedIndex+1))
                        Set_Measure(GlobalVar.motionConfig.vision_M1_list[cbx_m_RegionSelect.SelectedIndex]);
                }
            }
            else
            {
                if (cbx_m_RegionSelect.SelectedIndex > -1)
                {
                    if (GlobalVar.motionConfig.vision_M2_list != null && GlobalVar.motionConfig.vision_M2_list.Count > (cbx_m_RegionSelect.SelectedIndex + 1))
                        Set_Measure(GlobalVar.motionConfig.vision_M2_list[cbx_m_RegionSelect.SelectedIndex]);
                }
            }
            btn_m_ReadROI_Click(btn_m_ReadROI, null);
        }

        private void btn_m_plateTest_Click(object sender, EventArgs e)
        {
            if (GlobalVar.motionConfig.vision_M1_list != null && GlobalVar.motionConfig.vision_M1_list.Count > 2)
            {
                HOperatorSet.ReduceDomain(visionProc.srcObject_ckeck, visionProc.ROI_M_List1[(int)e_RegionCheck.切图区域], out HObject cutObj);
                HOperatorSet.Threshold(cutObj, out HObject threadObj, GlobalVar.motionConfig.cutParma1.GaryMin, GlobalVar.motionConfig.cutParma1.GaryMax);
                GlobalVar.imageDisplay3_obj(threadObj, "red");
                HOperatorSet.GetRegionPoints(threadObj, out HTuple rs, out HTuple cs);
                displalyInfo.displayInfoMain($"面积为{rs.Length}");
                //var vParam1 = GlobalVar.motionConfig.vision_M1_list[0];
                //var vParam2 = GlobalVar.motionConfig.vision_M1_list[1];
                //var vParam3 = GlobalVar.motionConfig.vision_M1_list[2];
                //var dis1=visionProc.GetMeasures(e_CameraPlate.检测相机平台1, vParam1);
                //var dis2 = visionProc.GetMeasures(e_CameraPlate.检测相机平台1, vParam2);
                //var dis3 = visionProc.GetMeasures(e_CameraPlate.检测相机平台1, vParam3);
                //displalyInfo.displayInfoMain("1:"+string.Join(",", dis1.ToArray()));
                //if (dis1.Where(a => a >= vParam1.distanceMin && a <= vParam1.distanceMax).Count() == vParam1.nPar)
                //    displalyInfo.displayInfoMain("1OK");
                //else
                //    displalyInfo.displayErrorMain("1NG");
                //displalyInfo.displayInfoMain("2:" + string.Join(",", dis2.ToArray()));
                //if (dis2.Where(a => a >= vParam2.distanceMin && a <= vParam2.distanceMax).Count() == vParam2.nPar)
                //    displalyInfo.displayInfoMain("2OK");
                //else
                //    displalyInfo.displayErrorMain("2NG");
                //displalyInfo.displayInfoMain("3:" + string.Join(",", dis3.ToArray()));
                //if (dis3.Where(a => a >= vParam3.distanceMin && a <= vParam3.distanceMax).Count() == vParam3.nPar)
                //    displalyInfo.displayInfoMain("3OK");
                //else
                //    displalyInfo.displayErrorMain("3NG");
            }
            if (GlobalVar.motionConfig.vision_M2_list != null && GlobalVar.motionConfig.vision_M2_list.Count > 2)
            {
                HOperatorSet.ReduceDomain(visionProc.srcObject_ckeck, visionProc.ROI_M_List2[(int)e_RegionCheck.切图区域], out HObject cutObj);
                HOperatorSet.Threshold(cutObj, out HObject threadObj, GlobalVar.motionConfig.cutParma1.GaryMin, GlobalVar.motionConfig.cutParma1.GaryMax);
                GlobalVar.imageDisplay3_obj(threadObj, "red");
                HOperatorSet.GetRegionPoints(threadObj, out HTuple rs, out HTuple cs);
                displalyInfo.displayInfoMain($"面积为{rs.Length}");
                
                //var vParam1 = GlobalVar.motionConfig.vision_M2_list[(int)e_RegionCheck.区域1];
                //var vParam2 = GlobalVar.motionConfig.vision_M2_list[(int)e_RegionCheck.区域2];
                //var vParam3= GlobalVar.motionConfig.vision_M2_list[(int)e_RegionCheck.区域3];
                //var dis1 = visionProc.GetMeasures(e_CameraPlate.检测相机平台2, vParam1);
                //var dis2 = visionProc.GetMeasures(e_CameraPlate.检测相机平台2, vParam2);
                //var dis3 = visionProc.GetMeasures(e_CameraPlate.检测相机平台2, vParam3);
                //displalyInfo.displayInfoMain("1:" + string.Join(",", dis1.ToArray()));
                //if(dis1.Where(a=>a >= vParam1.distanceMin&& a<= vParam1.distanceMax).Count()== vParam1.nPar)
                //    displalyInfo.displayInfoMain("1OK");
                //else
                //    displalyInfo.displayErrorMain("1NG");
                //displalyInfo.displayInfoMain("2:" + string.Join(",", dis2.ToArray()));
                //if (dis2.Where(a => a >= vParam2.distanceMin && a <= vParam2.distanceMax).Count() == vParam2.nPar)
                //    displalyInfo.displayInfoMain("2OK");
                //else
                //    displalyInfo.displayErrorMain("2NG");
                //displalyInfo.displayInfoMain("3:" + string.Join(",", dis3.ToArray()));
                //if (dis3.Where(a => a >= vParam3.distanceMin && a <= vParam3.distanceMax).Count() == vParam3.nPar)
                //    displalyInfo.displayInfoMain("3OK");
                //else
                //    displalyInfo.displayErrorMain("3NG");

            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            visionProc.curCameraPlate3 =GetSelect_M();
            GlobalVar.cameraList[(int)e_Camera.检测相机].softTrigger();
        }
        private void btn_In1_Click(object sender, EventArgs e)
        {
            var resString = GlobalVar.Box1.ReadBoxString((int)nud_r_StartAddress1.Value, (int)nud_r_dataLength.Value, (int)nud_r_station.Value, ckb_isReadWriteOK.Checked);
            displalyInfo.displayInfoMain(resString);
        }

        private void btn_In2_Click(object sender, EventArgs e)
        {
            var resString = GlobalVar.Box1.ReadBoxString((int)nud_r_StartAddress2.Value, (int)nud_r_dataLength.Value, (int)nud_r_station.Value, ckb_isReadWriteOK.Checked);
            displalyInfo.displayInfoMain(resString);
        }

        private void btn_Out1_Click(object sender, EventArgs e)
        {
            var resString = GlobalVar.Box2.WriteBoxString((int)nud_r_StartAddress1.Value, tbx_r_Content.Text.Trim(), (int)nud_r_station.Value, ckb_isReadWriteOK.Checked);
            displalyInfo.displayInfoMain(resString);
        }

        private void btn_Out2_Click(object sender, EventArgs e)
        {
            var resString = GlobalVar.Box2.WriteBoxString((int)nud_r_StartAddress2.Value, tbx_r_Content.Text.Trim(), (int)nud_r_station.Value, ckb_isReadWriteOK.Checked);
            displalyInfo.displayInfoMain(resString);
        }
        private void btn_In1_write_Click(object sender, EventArgs e)
        {
            var resString = GlobalVar.Box1.WriteBoxString((int)nud_r_StartAddress1.Value, tbx_r_Content.Text.Trim(), (int)nud_r_station.Value, ckb_isReadWriteOK.Checked);
            displalyInfo.displayInfoMain(resString);
        }

        private void btn_In2_write_Click(object sender, EventArgs e)
        {
            var resString = GlobalVar.Box1.WriteBoxString((int)nud_r_StartAddress2.Value, tbx_r_Content.Text.Trim(), (int)nud_r_station.Value, ckb_isReadWriteOK.Checked);
            displalyInfo.displayInfoMain(resString);
        }

        private void btn_Out1_read_Click(object sender, EventArgs e)
        {
            var resString = GlobalVar.Box2.ReadBoxString((int)nud_r_StartAddress1.Value, (int)nud_r_dataLength.Value, (int)nud_r_station.Value, ckb_isReadWriteOK.Checked);
            displalyInfo.displayInfoMain(resString);
        }

        private void btn_Out2_read_Click(object sender, EventArgs e)
        {
            var resString = GlobalVar.Box2.ReadBoxString((int)nud_r_StartAddress2.Value, (int)nud_r_dataLength.Value, (int)nud_r_station.Value, ckb_isReadWriteOK.Checked);
            displalyInfo.displayInfoMain(resString);
        }

        private void btn_RefreshPut_Click(object sender, EventArgs e)
        {
            if (rbn_v_plate1.Checked)
            {
                if (visionProc.realResurt1 != null)
                {
                    var temp = new VisionPos() { X = visionProc.realResurt1.X, Y = visionProc.realResurt1.Y, R = -visionProc.realResurt1.Angle };
                    set_VisionPut(temp);
                   
                }
                if(visionProc.BasePostion1!=null)
                {
                    var temp = new VisionPos() { X = visionProc.BasePostion1.X, Y = visionProc.BasePostion1.Y, R = -visionProc.BasePostion1.Angle };
                    set_VisionBase(temp);
                }
            }
            else
            {
                if (visionProc.ResultPlate2 != null)
                {
                    var temp = new VisionPos() { X = visionProc.realResurt2.X, Y = visionProc.realResurt2.Y, R = -visionProc.realResurt2.Angle };
                    set_VisionPut(temp);
                }
                if (visionProc.BasePostion2 != null)
                {
                    var temp = new VisionPos() { X = visionProc.BasePostion2.X, Y = visionProc.BasePostion2.Y, R = -visionProc.BasePostion2.Angle };
                    set_VisionBase(temp);
                }
            }
        }

        private LindFindParam Get_LindFindParam(LindFindParam Lparam)
        {
            if(Lparam==null)
                Lparam = new LindFindParam();
            Lparam.dLength = (double)nud_tL_RulerLength.Value;
            Lparam.dSigma = (double)nud_tL_Sigma.Value;
            Lparam.dWidth = (double)nud_tL_RulerWidth.Value;
            Lparam.Threshold = (int)nud_tL_thread.Value;
            return Lparam;
        }
        private LindFindParam Set_LindFindParam(LindFindParam param)
        {
            if(param==null)
            param = new LindFindParam();
            nud_tL_RulerLength.Value = (decimal)param.dLength;
            nud_tL_Sigma.Value = (decimal)param.dSigma;
            nud_tL_RulerWidth.Value = (decimal)param.dWidth;
            nud_tL_thread.Value = (decimal)param.Threshold;
            return param;
        }
        private CircleFindParam Get_CircleFindParam(CircleFindParam Lparam)
        {
            if (Lparam == null)
                Lparam = new CircleFindParam();
            
            //Lparam.dSigma = (double)nud_tc_sigma.Value;
            //Lparam.dHalfWidth = (double)nud_tc_width.Value;
            //Lparam.dHalfLength = (double)nud_tc_length.Value;
            //Lparam.dThreshold = (int)nud_tc_Threshold.Value;
            //Lparam.dRadius = (double)nud_tc_radius.Value;
            //Lparam.point_order =cbx_tc_pointOrder.Text;
            //Lparam.dStartAngle = (double)nud_tc_startAngle.Value;
            //Lparam.dEndAngle = (double)nud_tc_EndAngle.Value;
            return Lparam;
        }
        private void Set_CircleFindParam(CircleFindParam param)
        {
            if (param == null)
                param = new CircleFindParam();
           
            //nud_tc_sigma.Value = (decimal)param.dSigma;
            //nud_tc_width.Value = (decimal)param.dHalfWidth;
            //nud_tc_length.Value = (decimal)param.dHalfLength;
            //nud_tc_Threshold.Value = (decimal)param.dThreshold;
            //nud_tc_radius.Value = (decimal)param.dRadius;
            // cbx_tc_pointOrder.Text= param.point_order;
            //nud_tc_startAngle.Value = (decimal)param.dStartAngle;
            //nud_tc_EndAngle.Value = (decimal)param.dEndAngle;
        }
        private void Set_CircleParam(CircleFindParam param)
        {
            if (param == null)
                param = new CircleFindParam();

            nud_c_sigma.Value = (decimal)param.dSigma;
            nud_c_width.Value = (decimal)param.dHalfWidth;
            nud_c_length.Value = (decimal)param.dHalfLength;
            nud_c_Threshold.Value = (decimal)param.dThreshold;
            nud_c_radius.Value = (decimal)param.dRadius;
            cbx_c_pointOrder.Text = param.point_order;
            nud_c_startAngle.Value = (decimal)param.dStartAngle;
            nud_c_EndAngle.Value = (decimal)param.dEndAngle;
        }
        private CircleFindParam Get_CircleParam(CircleFindParam Lparam)
        {
            if (Lparam == null)
                Lparam = new CircleFindParam();

            Lparam.dSigma = (double)nud_c_sigma.Value;
            Lparam.dHalfWidth = (double)nud_c_width.Value;
            Lparam.dHalfLength = (double)nud_c_length.Value;
            Lparam.dThreshold = (int)nud_c_Threshold.Value;
            Lparam.dRadius = (double)nud_c_radius.Value;
            Lparam.point_order = cbx_c_pointOrder.Text;
            Lparam.dStartAngle = (double)nud_c_startAngle.Value;
            Lparam.dEndAngle = (double)nud_c_EndAngle.Value;
            return Lparam;
        }
        private VisionPos Get_VisionPut()
        {

            var put = new VisionPos();
            put.X = (double)nud_VisionPutX.Value;
            put.Y = (double)nud_VisionPutY.Value;
            put.R = (double)nud_VisionPutR.Value;
            return put;
        }
        private void set_VisionPut(VisionPos put)
        {
            if (put == null)
                put = new VisionPos();
            nud_VisionPutX.Value = (decimal)put.X;
            nud_VisionPutY.Value = (decimal)put.Y;
            nud_VisionPutR.Value = (decimal)put.R;
        }
        private VisionPos Get_VisionBase()
        {

            var basePos = new VisionPos();
            basePos.X = (double)nud_baseX.Value;
            basePos.Y = (double)nud_baseY.Value;
           
            return basePos;
        }
     
        private void set_VisionBase(VisionPos basePos)
        {
            if (basePos == null)
                basePos = new VisionPos();
            nud_baseX.Value = (decimal)basePos.X;
            nud_baseY.Value = (decimal)basePos.Y;
        
        }

        private void btn_Move_NGlabelZ_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标Z轴].PointMove((double)nud_NGLabelZ.Value, true);
        }

        private void btn_move_OKZHeight_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标Z轴].PointMove((double)nud_pos_OKZHeight.Value, true);
        }

        private void btn_Move_NGZHeight_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标Z轴].PointMove((double)nud_pos_NGZHeight.Value, true);
        }

       

        private void btn_Move_GetLabelZHeight_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标Z轴].PointMove((double)nud_GetLabelZHeight.Value, true);
        }

        private void btn_testPos_Click(object sender, EventArgs e)
        {
            GlobalVar.IsPosBeforeLabel = true;
            GlobalVar.IsLabelPosTwo = true;
            GlobalVar.cameraList[(int)e_Camera.检测相机].softTrigger();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            GlobalVar.IsPosBeforeLabel = true;
           visionProc. visionProcCam(visionProc.srcObject_ckeck, e_CameraPlate.检测相机平台1);
            visionProc.visionProcCam(visionProc.srcObject_ckeck, e_CameraPlate.检测相机平台2);
            GlobalVar.IsPosBeforeLabel = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (rbn_m_Plate1.Checked)
            {
                if (visionProc.pos1pix != null)
                {
                    nud_p_realR1.Value = (decimal)visionProc.pos1pix.R;
                    nud_p_realX1.Value = (decimal)visionProc.pos1pix.X;
                    nud_p_realY1.Value = (decimal)visionProc.pos1pix.Y;
                }
            }
            else
            {
                if (visionProc.pos2pix != null)
                {
                    nud_p_realR2.Value = (decimal)visionProc.pos2pix.R;
                    nud_p_realX2.Value = (decimal)visionProc.pos2pix.X;
                    nud_p_realY2.Value = (decimal)visionProc.pos2pix.Y;
                }
            }
           
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        private void groupBox24_Enter(object sender, EventArgs e)
        {

        }

        private void btn_LineTest_Click(object sender, EventArgs e)
        {
        
          

        }

        private void btn_TestLine_Click(object sender, EventArgs e)
        {
           
        }

        private void button4_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog ofg = new FolderBrowserDialog();
            ofg.Description = "选择文件夹";
            ofg.SelectedPath = Environment.CurrentDirectory;
            var res = ofg.ShowDialog();
            if (res == DialogResult.OK)
            {
                string paths = ofg.SelectedPath;
                var fileName = Directory.GetFiles(paths, "*.jpg");
                for (int i = 0; i < fileName.Length; i++)
                {
                    var nIndex = fileName[i].LastIndexOf("\\");
                    var strPath = fileName[i].Substring(nIndex + 1);
                }
                DirectoryInfo infos = new DirectoryInfo(paths);
                var fInfos = infos.GetFiles();
                var files = fInfos.Where(a => a.FullName.Contains(".jpeg") || a.FullName.Contains(".jpg") || a.FullName.Contains(".bmp") || a.FullName.Contains(".png")).Select(a => a.FullName).ToList();
                foreach (var f in files)
                {
                    HOperatorSet.ReadImage(out visionProc.srcObject_ckeck, f);
                    GlobalVar.imageDisplay3(visionProc.srcObject_ckeck);

                    // var line1 = visionProc.findMeasureLine(visionProc.srcObject_ckeck, GlobalVar.motionConfig.LindFind1[(int)e_Line.直线1], visionProc.ROI_M_List1[(int)e_RegionCheck.直线区域1]);
                    //HOperatorSet.Emphasize (visionProc.srcObject_ckeck, out visionProc.srcObject_ckeck, 17, 17,17);
                    // var line2 = visionProc.findMeasureLine(visionProc.srcObject_ckeck, GlobalVar.motionConfig.LindFind1[(int)e_Line.直线2], visionProc.ROI_M_List1[(int)e_RegionCheck.直线区域2]);
                    // var pos = visionProc.intersection_Line(line1, line2);
                    // if (pos != null)
                    // {
                    //     displalyInfo.displayErrorMain($"X:{pos.X.ToString("F04")}Y:{pos.Y.ToString("F04")}");
                    // }
                    HOperatorSet.RotateImage(visionProc.srcObject_ckeck, out visionProc.srcObject_ckeck, 90, "constant");
                    var curSelect = GetSelect_T();
                    var imgDir = Environment.CurrentDirectory + $"\\saveImage\\{curSelect.ToString()}\\大切图6";
                    if (!Directory.Exists(imgDir))
                    {
                        Directory.CreateDirectory(imgDir);
                    }
                    HOperatorSet.WriteImage(visionProc.srcObject_ckeck, "jpeg", 0, imgDir + $"\\{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff")}.jpeg");
                }

            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            var curSelect = GetSelect_T();
            HOperatorSet.ReduceDomain(visionProc.srcObject_pos,visionProc.ROI_List[(int)e_CameraPlate.定位相机平台1].ROI_Region, out HObject TEMP);
            //visionProc.scale_image_range(visionProc.srcObject_pos, out visionProc.srcObject_pos, 100, 200);
            HOperatorSet.Threshold(TEMP, out HObject white, 180, 255);
            HOperatorSet.Threshold(TEMP, out HObject black, 0, 180);
            HOperatorSet.GetRegionPoints(white, out HTuple rows1, out HTuple cols1);
            HOperatorSet.GetRegionPoints(black, out HTuple rows2, out HTuple cols2);
            for (int i = 0; i < rows1.LArr.Length; i++)
            {
                HOperatorSet.SetGrayval(visionProc.srcObject_pos, rows1.LArr[i], cols1.LArr[i], 220);
            }
            for (int i = 0; i < rows2.LArr.Length; i++)
            {
                HOperatorSet.SetGrayval(visionProc.srcObject_pos, rows2.LArr[i], cols2.LArr[i], 5);
            }

            var imgDir = Environment.CurrentDirectory + $"\\saveImage\\{curSelect.ToString()}\\新透明标定切图";
            if (!Directory.Exists(imgDir))
            {
                Directory.CreateDirectory(imgDir);
            }
            HOperatorSet.WriteImage(visionProc.srcObject_pos, "jpeg", 0, imgDir + $"\\{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff")}.jpeg");
        }

        private void button6_Click(object sender, EventArgs e)
        {
          
            HOperatorSet.GenRectangle1(out HObject rectangle, 0,350, 3650, 5250);
            HOperatorSet.ReduceDomain(visionProc.srcObject_pos, rectangle, out HObject TEMP);
            HOperatorSet.CropDomain(TEMP, out HObject cropHobject);

            var curSelect = GetSelect_T();
            var imgDir = Environment.CurrentDirectory + $"\\saveImage\\{curSelect.ToString()}\\大切图5";
            if (!Directory.Exists(imgDir))
            {
                Directory.CreateDirectory(imgDir);
            }
            HOperatorSet.WriteImage(cropHobject, "jpeg", 0, imgDir + $"\\{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff")}.jpeg");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            HTuple hv_CameraParameters = new HTuple();
            HTuple hv_CameraPose = new HTuple();
            hv_CameraParameters[0] = "area_scan_division";
            hv_CameraParameters[1] = 0.000113164;
            hv_CameraParameters[2] = -2018.81;
            hv_CameraParameters[3] = 2.40052e-06;
            hv_CameraParameters[4] = 2.4e-06;
            hv_CameraParameters[5] = 2192.92;
            hv_CameraParameters[6] = 1706.05;
            hv_CameraParameters[7] = 4401;
            hv_CameraParameters[8] = 3648;
 
            hv_CameraPose = new HTuple();
            hv_CameraPose[0] = 0.00857587;
            hv_CameraPose[1] = -00129801;
            hv_CameraPose[2] = 0.00465927;
            hv_CameraPose[3] = 359.993;
            hv_CameraPose[4] = 359.999;
            hv_CameraPose[5] = 90.8959;
            hv_CameraPose[6] = 0;

            var hv_CameraZero = new HTuple();
            hv_CameraZero[0] = 0;
            hv_CameraZero[1] = 0;
            hv_CameraZero[2] = 0;
            hv_CameraZero[3] = 0;
            hv_CameraZero[4] = 0;


            HOperatorSet.ChangeRadialDistortionCamPar("adaptive", hv_CameraParameters, 0, out HTuple CarParamVirtualFixed);
            HOperatorSet.GenRadialDistortionMap(out HObject ho_TmpObj_RectificationMap, hv_CameraParameters, CarParamVirtualFixed, "bilinear");
//            change_radial_distortion_cam_par('adaptive', CameraParam,[0, 0, 0, 0, 0], CarParamVirtualFixed)


//* 上述相机模型选一种后进行下面的map_image
//* 创建一个投射图，其描述图像与其相应正在改变的径向畸变，而对于12个参数的畸变包括径向和切向畸变
//gen_radial_distortion_map(Map, CameraParam, CarParamVirtualFixed, 'bilinear')

//            HOperatorSet.GenImageToWorldPlaneMap(out HObject ho_TmpObj_RectificationMap, hv_CameraParameters,
//        hv_CameraPose, 4401, 3648, 4401, 3648, 1,"bilinear");
            HOperatorSet.MapImage(visionProc.srcObject_pos, ho_TmpObj_RectificationMap, out HObject newiMAGE);
            GlobalVar.imageDisplay1(newiMAGE);
        }

        private void cbo_selectVersion_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbo_type_selectType.SelectedIndex > -1&&cbo_selectVersion.SelectedIndex > -1)
            {
                var strSelect = cbo_type_selectType.Text;
                var strVersion = cbo_selectVersion.Text;
                var tList = GlobalVar.tv_list.Where(a => a.type == strSelect&& a.Version == strVersion).ToList();
                if (tList.Count > 0)
                {
                    var curtypeVerison = tList[0].type + "~" + tList[0].Version;
                    GlobalVar.motionConfig = (MotionConfig)SerializeHelper.DeserializeFile<MotionConfig>(GlobalVar.type_path + "\\" + curtypeVerison + ".tfp", "型号参数");//运动参数反序列化
                    ParameterToControl();
                    GlobalVar.closeSave.CurrentTypeName = curtypeVerison;
                    displalyInfo.displayInfoMain($"版本切换为{ GlobalVar.closeSave.CurrentTypeName }");
                }
            }
        }

        private void btn_displayCenter_Click(object sender, EventArgs e)
        {
            var xPix = nud_VisionCenterX.Value;
            var yPix = nud_VisionCenterY.Value;
            if (rbn_v_plate1.Checked)
            {
                if (visionProc.Mark1Pix != null)
                {
                    HOperatorSet.GenCrossContourXld(out HObject CenterObj, visionProc.Mark1Pix.X + (double)xPix, visionProc.Mark1Pix.X + (double)yPix, 200, 0);
                    GlobalVar.imageDisplay1_obj(CenterObj, "blue");
                }
            }
            else
            {
                if (visionProc.Mark2Pix != null)
                {
                    HOperatorSet.GenCrossContourXld(out HObject CenterObj, visionProc.Mark2Pix.X + (double)xPix, visionProc.Mark2Pix.X + (double)yPix, 200, 0);
                    GlobalVar.imageDisplay1_obj(CenterObj, "blue");
                }
            }
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void cbx_Region_SelectedIndexChanged(object sender, EventArgs e)
        {
            var curPlate = GetSelect_T();
            if (cbx_Region.SelectedIndex>=(int)e_Region.直线区域1 && cbx_Region.SelectedIndex <= (int)e_Region.直线区域2)
            {
               
                if(curPlate==e_CameraPlate.定位相机平台1)
                {
                    if (GlobalVar.motionConfig.PosLindFind1 != null && GlobalVar.motionConfig.PosLindFind1.Count >= (cbx_Region.SelectedIndex - (int)e_Region.直线区域1))
                    {
                        var lineParama = GlobalVar.motionConfig.PosLindFind1[cbx_Region.SelectedIndex - (int)e_Region.直线区域1];
                        Set_LindFindParam(lineParama);
                    }
                }
                 else if (curPlate == e_CameraPlate.定位相机平台2)
                {
                    if (GlobalVar.motionConfig.PosLindFind2 != null && GlobalVar.motionConfig.PosLindFind2.Count >= (cbx_Region.SelectedIndex- (int)e_Region.直线区域1))
                    {
                        var lineParama = GlobalVar.motionConfig.PosLindFind2[cbx_Region.SelectedIndex - (int)e_Region.直线区域1];
                        Set_LindFindParam(lineParama);
                    }
                }
            }else if(cbx_Region.SelectedIndex >= (int)e_Region.抓圆区域)
            {
                if (curPlate == e_CameraPlate.定位相机平台1)
                {
                    if (GlobalVar.motionConfig.PosCircleFindCheck1 != null && GlobalVar.motionConfig.PosCircleFindCheck1.Count >= (cbx_Region.SelectedIndex - (int)e_Region.抓圆区域))
                    {
                        var lineParama = GlobalVar.motionConfig.PosCircleFindCheck1[cbx_Region.SelectedIndex - (int)e_Region.抓圆区域];
                        Set_CircleFindParam(lineParama);
                    }
                }
                else if (curPlate == e_CameraPlate.定位相机平台2)
                {
                    if (GlobalVar.motionConfig.PosCircleFindCheck2 != null && GlobalVar.motionConfig.PosCircleFindCheck2.Count >= (cbx_Region.SelectedIndex - (int)e_Region.抓圆区域))
                    {
                        var lineParama = GlobalVar.motionConfig.PosCircleFindCheck2[cbx_Region.SelectedIndex - (int)e_Region.抓圆区域];
                        Set_CircleFindParam(lineParama);
                    }
                }
            }
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            
           visionProc.visionProcCam(visionProc.srcObject_pos, GetSelect_T());
        }

        private void btn_move_CheckY_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.贴标Y轴].PointMove((double)nud_checkPosY.Value, true);
        }

        private void btn_cut_NGZHeight_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜Z].PointMove((double)nud_cut_NGZHeight.Value, true);
        }

        private void btn_cut_NGZLow_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜Z].PointMove((double)nud_cut_NGZLow.Value, true);
        }

        private void btn_cut_SafeX_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜X].PointMove((double)nud_cut_SafeX.Value, true);
        }

        private void btn_cut_SafeY_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜Y].PointMove((double)nud_cut_SafeY.Value, true);
        }

        private void btn_cut_SafeZ_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜Z].PointMove((double)nud_cut_SafeZ.Value, true);
        }

        private void btn_cut_SafeR1_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜R1].PointMove((double)nud_cut_SafeR1.Value, true);
        }

        private void btn_cut_SafeR2_Click(object sender, EventArgs e)
        {
            GlobalMotion.Card_1.AxisList[(int)e_card1_axis.撕膜R2].PointMove((double)nud_cut_SafeR2.Value, true);
        }
    }
    public class Limit
    {
        public static int GetLabelXmax = 0;
        public static int GetLabelXmin = -100;
        public static int GetLabelYmax = 50;
        public static int GetLabelYmin = 0;
        public static int GetLabelRmax = 4;
        public static int GetLabelRmin = -30;
        public static int GetLabelZmax = 40;
        public static int GetLabelZmin = 0;

        public static int Pos_ScanXmax = -100;
        public static int Pos_ScanXmin = -210;
        public static int Pos_ScanYmax = 300;
        public static int Pos_ScanYmin = 100;
        public static int Pos_ScanZmax = 10;
        public static int Pos_ScanZmin = 0;

        public static int PosNGX_Max = 0;
        public static int PosNGX_Min = -50;
        public static int PosNGY_Max = 300;
        public static int PosNGY_Min = 100;
        public static int PosNGZ_Max = 190;
        public static int PosNGZ_Min = 0;
        public static int PosNGZHeight_Max = 30;
        public static int PosNGZHeight_Min = 0;
        public static int PosNGR_Max = 0;
        public static int PosNGR_Min = -100;

        public static int PosOKX_Max = 0;
        public static int PosOKX_Min = -210;
        public static int PosOKY_Max = 550;
        public static int PosOKY_Min = 500;
        public static int PosOKZ_Max = 230;
        public static int PosOKZ_Min = 100;
        public static int PosOKZHeight_Max = 100;
        public static int PosOKZHeight_Min = 0;
        public static int PosOKR_Max = 0;
        public static int PosOKR_Min = -100;
    }
}
